BookmarkSubscribeRSS Feed
kennyA
Calcite | Level 5

I want to ask how to properly use if then statements.

lets say the variables is score.

score<50=fail_score, score 50-75=decent_score 75-100=good_score

What should the if statements be?

 

if score<=50 then output fail_score;

If 50>score<=75 then output decent_score;

if 75>score<=100 then output good_score?

3 REPLIES 3
kennyA
Calcite | Level 5
do i need to "keep fail_score decent_score good_score;" before running?
FreelanceReinh
Jade | Level 19

@kennyA wrote:

I want to ask how to properly use if then statements.

lets say the variables is score.

score<50=fail_score, score 50-75=decent_score 75-100=good_score

What should the if statements be?

 

if score<=50 then output fail_score;

If 50>score<=75 then output decent_score;

if 75>score<=100 then output good_score?


Hi @kennyA,

 

First of all, it's advisable to check the specifications. In your example they are ambiguous regarding the classification of score=75. Let's fix this for now: score 50 - <75 = decent_score.

 

Typically such ranges would be dealt with from bottom to top or vice versa including ELSE statements in order to avoid typing the same boundary value twice. Always remember that in SAS an inequality like score<=50 is not only met by score values between 0 and 50 and negative values, but also by missing (incl. special missing) values of score. Moreover, the possibility of unexpected values (e.g. score>100) should be kept in mind. (In a more advanced context even the possibility of rounding errors should be considered, e.g., score=50.0000000000001 with the deviation from 50 being an artifact created by a calculation involving rounding errors, possibly due to numeric representation issues.)

 

Bottom-up approach:

if missing(score) then ...; /* equivalent: if score<=.z then ...; */
else if score<50 then ...;
else if score<75 then ...;
else if score<=100 then ...;
else ...; /* handle values >100 if any */

Corresponding top-down approach:

if score>100 then ...;
else if score>=75 then ...;
else if score>=50 then ...;
else if ~missing(score) then ...; /* equivalent: else if score>.z then ...; */
else ...; /* handle missing values */

(.z is the largest special missing value.)

 

Note that your IF conditions are incorrect. The first doesn't match the specification for score=50 (and includes missing scores in the selection if any), the second is wrongly equivalent to score<50 and the third to score<75.

 

Also it's unclear what the actions should be: Create new variable(s)? Write something to the log? Create new datasets? ...

 

Your OUTPUT statments would be suitable for creating new datasets containing observations satisfying the respective IF conditions. In this case their names (fail_score etc.) must appear in the DATA statement of the DATA step, not in a KEEP statement.

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!

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
  • 3 replies
  • 545 views
  • 0 likes
  • 3 in conversation