After a three-year haitus since completing my masters in data analytics, health and life sciences, I am jumping back in and using my old grad school book, Applied Statistics and the SAS Programming Language(Cody et al, 2006). I am running into a problem with the IF/THEN ELSE IF statements and it makes no sense. I guess the book could be wrong, but I am entering everything the books says to enter. I am getting this error:
Here is my code:
PROC FORMAT;
VALUE $SEXFMT '1' = 'MALE' '2' = 'FEMALE' '3' = 'MISCODED';
VALUE $RACE '1' = 'WHITE' '2' = 'AFRICAN AMERICAN' '3' = 'HISPANIC' '4' = 'OTHER';
VALUE $OSCAR '1' = 'SINGLE' '2' = 'MARRIED' '3' = 'WIDOWED' '4' = 'DIVORCED';
VALUE $EDUC '1' = 'HIGH SCHOOL OR LESS' '2' = 'TWO-YEAR COLLEGE' '3' = 'FOUR-YEAR COLLEGE' '4' = 'GRADUATE COLLEGE';
VALUE $LIKERT 1 = 'STRONGLY DISAGREE' 2 = 'DISAGREE' 3 = 'NO OPINION' 4 = 'AGREE' 5 = 'STRONGLY AGREE';
VALUE AGEFMT 1 = '0-20' 2 = '21-40' 3 = '41-60' 4 = '>60';
RUN;
DATA QUEST;
INPUT ID $ 1-3
AGE 4-5
GENDER $ 6
RACE $ 7
MARITAL $ 8
EDUCATION $ 9
PRESIDENT $ 10
ARMS $ 11
CITIES $ 12;
IF AGE GE 0 AND AGE LE 20 THEN AGEGRP = 1;
ELSE IF AGE GT 20 AND AGE LE 40 THEN AGEGRP = 2;
ELSE IF AGE GT 40 AND AGE LE 60 THEN AGEGRP = 3;
ELSE IF AGE GT 60 THEN AGEGRP = 4;
*CLARIFY TABLES;
LABEL MARITAL = 'MARITAL STATUS'
EDUCATION = 'EDUCATION LEVEL'
PRESIDENT = 'PRESIDENT DOING A GOOD JOB'
ARMS = 'ARMS BUDGET INCREASE'
CITIES = 'FEDERAL AID TO CITIES'
AGEGRP = 'AGE GROUP';
FORMAT GENDER $SEXFMT.
RACE $RACE.
MARITAL $OSCAR.
EDUCATION $EDUC.
PRESIDENT ARMS CITIES LIKERT.;
DATALINES;
001091111232
002452222422
003351324442
004271111121
005682132333
006651243425
;
PROC MEANS DATA=QUEST MAXDEC=2 N MEAN STD CLM;
TITLE "QUESTIONNAIRE ANALYSIS";
VAR AGE;
FORMAT AGEGRP AGEFMT.;
RUN;
PROC FREQ DATA=QUEST;
TITLE "FREQUENCY COUNTS FOR CATEGORICAL VARIABLES";
TABLES GENDER -- AGEGRP; *VARS MUST BE IN ORDER TO USE THE -- SHORTCUT;
RUN;
I know I am doing something stupid. Any help would be great.
I forgot the condition in the first if statement.
**disregard**
I caught the $LIKERT error and changed that to LIKERT, but it isn't the problem.
I forgot the condition in the first if statement.
**disregard**
I'm a stickler for clean code.
You may want to consider this for the "if/then/else" block.
IF 0 LE AGE LE 20 THEN AGEGRP = 1; ELSE IF 20 LE AGE LE 40 THEN AGEGRP = 2; ELSE IF 40 LE AGE LE 60 THEN AGEGRP = 3; ELSE IF AGE GT 60 THEN AGEGRP = 4;
SAS will allow multiple comparisons such as 23 < var1 < 45 <var2 < 127;
(if var1 is in interval (23,45) AND var1<var2 AND var2 in interval (45, 127) as one interpretation)
Which for some cases can greatly simplify code without having to get the interactions between and and or straight.
I find a left to right comparison very useful with date values especially such as a begin date after a given period and end date after the begin but before yet another boundary date, conceptually:
reportperiodstart le begindate le enddate le reportperiodend
as an example.
SAS is one of the few languages that allows this sort of comparison with multiple variables/values easily.
This is great, but for readability sake I think the standard if/else/then statement works when coming back to old code or making sure that code can last through multiple devs/statisticians. Just a personal preference, but your way has merit in a lot of cases.
@abbyrjones72 wrote:
This is great, but for readability sake I think the standard if/else/then statement works when coming back to old code or making sure that code can last through multiple devs/statisticians. Just a personal preference, but your way has merit in a lot of cases.
One thing is that with the relative positions and operators, when appropriate logic, I think it is usually easier to see the logic, at least if the code is lined up nicely.
This forum has a number of similar " if a>0 and a<15" type questions where one or more condition needed to be changed but was done incorrectly because finding the position in 10 or 15 lines of code (which often is not well structured to read) was difficult.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.