BookmarkSubscribeRSS Feed
Banke
Pyrite | Level 9

Hello all,

 

i want to create a frequencyt table for my newly categorized variables. there is no error in log but when i run the proc freq , it doesnt show all the categories i created for the first variable which is race. it showed just 2 instead of 5

here is my syntax

 

DATA MEPSIS.RACEDU;
SET MEPSIS.TEMP5;

IF RACE = 1 THEN RACECAT = 'HISPANIC';
IF RACE = 2 THEN RACECAT = 'NON-HISPANIC WHITE ONLY';
IF RACE = 3 THEN RACECAT = 'NON-HISPANIC BLACK ONLY';
IF RACE = 4 THEN RACECAT = 'NON-HISPANIC ASIAN ONLY';
IF RACE = 5 THEN RACECAT = 'NON-HISPANIC OTHER RACE OR MULTI-RACE';
IF RACE < 1 THEN DELETE;


IF EDUCATION = 1 THEN EDUCAT = '=<8TH GRADE';
IF EDUCATION = 2 THEN EDUCAT = '9TH TO 12TH GRADE';
IF EDUCATION = 13 THEN EDUCAT = 'GED OR HIGH SCHOOL DIPLOMA';
IF EDUCATION = 14 THEN EDUCAT = '>HIGH SCHOOL BUT WITHOUT 4 YEAR DEGREE';
IF EDUCATION = 15 THEN EDUCAT = 'COLLEGE OR BACHELORS DEGREE';
IF EDUCATION = 16 THEN EDUCAT = 'GRADUATE OR PROFESSIONAL DEGREE';
IF EDUCATION < 1 THEN DELETE;

RUN;


/** PRINTING TO CHECK**/
PROC PRINT DATA = MEPSIS.RACEDU (OBS = 5);
VAR RACECAT EDUCAT;
TITLE "CROSSCHECKING QUESTION 4";
RUN;

 

 

7 REPLIES 7
Shmuel
Garnet | Level 18

Is it possible that one of the two conditions is true:

IF RACE < 1 THEN DELETE;

IF EDUCATION < 1 THEN DELETE;

 

Check the missing observations, maybe they satisfies above situation.

I propose to replace the DELETE with '***';

Astounding
PROC Star
These statements define RACECAT as 8 characters long, the number of characters in "HISPANIC". Other values get truncated. To fix this, add just after the SET statement:

LENGTH RACECAT $37;
Reeza
Super User
  1. DATA MEPSIS.RACEDU;
    SET MEPSIS.TEMP5;
    
    length RACECAT EDUCAT $50.;
    
    IF RACE = 1 THEN RACECAT = 'HISPANIC';
    ELSE IF RACE = 2 THEN RACECAT = 'NON-HISPANIC WHITE ONLY';
    ELSE IF RACE = 3 THEN RACECAT = 'NON-HISPANIC BLACK ONLY';
    ELSE IF RACE = 4 THEN RACECAT = 'NON-HISPANIC ASIAN ONLY';
    ELSE IF RACE = 5 THEN RACECAT = 'NON-HISPANIC OTHER RACE OR MULTI-RACE';
    *ELSE IF RACE < 1 THEN DELETE;
    ELSE RACECAT="CHECKME";
    
    
    IF EDUCATION = 1 THEN EDUCAT = '=<8TH GRADE';
    ELSE IF EDUCATION = 2 THEN EDUCAT = '9TH TO 12TH GRADE';
    ELSE IF EDUCATION = 13 THEN EDUCAT = 'GED OR HIGH SCHOOL DIPLOMA';
    ELSE IF EDUCATION = 14 THEN EDUCAT = '>HIGH SCHOOL BUT WITHOUT 4 YEAR DEGREE';
    ELSE IF EDUCATION = 15 THEN EDUCAT = 'COLLEGE OR BACHELORS DEGREE';
    ELSE IF EDUCATION = 16 THEN EDUCAT = 'GRADUATE OR PROFESSIONAL DEGREE';
    ELSE IF EDUCATION < 1 THEN DELETE;
    ELSE EDUCAT = "CHECKME";
    
    RUN;
    
    
    
    proc freq data=mepsis.racedu;
    table race*racecat education*educat racecat*educat;
    run;
  2. You only printed 5 observations, how do you know that all categories weren't created?
  3. You say PROC FREQ but are showing a PROC PRINT, did you forget to include some of your code? You should check recoding with PROC FREQ not PROC PRINT. 
  4. I would recommend coding the categories to something else, instead of deleting, checking your output and then once you were satisfied, deleting the records. Delete only after your selection is verified. 

@Banke wrote:

Hello all,

 

i want to create a frequencyt table for my newly categorized variables. there is no error in log but when i run the proc freq , it doesnt show all the categories i created for the first variable which is race. it showed just 2 instead of 5

here is my syntax

 

DATA MEPSIS.RACEDU;
SET MEPSIS.TEMP5;

IF RACE = 1 THEN RACECAT = 'HISPANIC';
IF RACE = 2 THEN RACECAT = 'NON-HISPANIC WHITE ONLY';
IF RACE = 3 THEN RACECAT = 'NON-HISPANIC BLACK ONLY';
IF RACE = 4 THEN RACECAT = 'NON-HISPANIC ASIAN ONLY';
IF RACE = 5 THEN RACECAT = 'NON-HISPANIC OTHER RACE OR MULTI-RACE';
IF RACE < 1 THEN DELETE;


IF EDUCATION = 1 THEN EDUCAT = '=<8TH GRADE';
IF EDUCATION = 2 THEN EDUCAT = '9TH TO 12TH GRADE';
IF EDUCATION = 13 THEN EDUCAT = 'GED OR HIGH SCHOOL DIPLOMA';
IF EDUCATION = 14 THEN EDUCAT = '>HIGH SCHOOL BUT WITHOUT 4 YEAR DEGREE';
IF EDUCATION = 15 THEN EDUCAT = 'COLLEGE OR BACHELORS DEGREE';
IF EDUCATION = 16 THEN EDUCAT = 'GRADUATE OR PROFESSIONAL DEGREE';
IF EDUCATION < 1 THEN DELETE;

RUN;


/** PRINTING TO CHECK**/
PROC PRINT DATA = MEPSIS.RACEDU (OBS = 5);
VAR RACECAT EDUCAT;
TITLE "CROSSCHECKING QUESTION 4";
RUN;

 

 


 

Banke
Pyrite | Level 9

ok. thank you. i uploaded the wrong picture, sorry about that. It actually created all the categories i wanted, i scanned through the dataset. i just observed that it grouped the categories into 2 instead of 5 in the frequency table whereas there are 5 categories in the created dataset. what does the "CHECKME" mean please?

 

 

tarheel13
Rhodochrosite | Level 12
She recoded instead of deleting. Also I think you should use if else if instead of just a bunch of if statements.
Reeza
Super User

@Banke wrote:

ok. thank you. i uploaded the wrong picture, sorry about that. It actually created all the categories i wanted, i scanned through the dataset. i just observed that it grouped the categories into 2 instead of 5 in the frequency table whereas there are 5 categories in the created dataset. what does the "CHECKME" mean please?

 

 


It literally means go check those records. That is because they're falling out of your IF/ELSE conditions which means you're not accounting for some conditions or deletions. Coding it to a value rather than delete allows you to verify the logic before you delete it. This style of programming helps you to find errors in your code before they propogate.

 

 

tarheel13
Rhodochrosite | Level 12

The proper way to check this variable derivation is like this:

proc freq data=mepsis.racedu;
tables race*racecat / list missing;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1331 views
  • 6 likes
  • 5 in conversation