BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abbyrjones72
Obsidian | Level 7

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:

abbyrjones72_0-1626375350136.png

 

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
abbyrjones72
Obsidian | Level 7

I forgot the condition in the first if statement.

**disregard**

View solution in original post

7 REPLIES 7
abbyrjones72
Obsidian | Level 7

I caught the $LIKERT error and changed that to LIKERT, but it isn't the problem.

abbyrjones72
Obsidian | Level 7

I forgot the condition in the first if statement.

**disregard**

Reeza
Super User
lol! Not a dumb question by any means, and well formulated with actual sample data. The one thing I've learned myself from following that process is that I end up solving the problem before I even post it about 50% of the time when I first started and 80% of the time these days.
abbyrjones72
Obsidian | Level 7

I'm a stickler for clean code.

ballardw
Super User

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.

abbyrjones72
Obsidian | Level 7

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.

ballardw
Super User

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 891 views
  • 5 likes
  • 3 in conversation