Help using Base SAS procedures

SAS query

Reply
Contributor
Posts: 57

SAS query


Hi All,

I use this code:

data work.flag;

infile "C:\Documents and Settings\test\test.txt";

input name $ B;

if B = . then flag = 'Unknown';

else if B = 1 then flag = 'Low';

else if B = 2 or 3 then flag = 'Medium';

else flag = 'High';

PROC PRINT DATA = flag;

RUN;

My question is that when B = 4 then why SAS assigns it as Medium because I would expect that flag should be High for any values other than 1,2 and 3? But if I use  B = 2 or B = 3 then for B=4 the flag is assigned as High?

SK

Regular Contributor
Posts: 195

Re: SAS query

Posted in reply to Siddharth123

Hi,

In SAS, any numeric value other than 0 or missing is true, and a value of 0 or missing is false. Therefore, a numeric variable or expression can stand alone in a condition. If its value is a number other than 0 or missing, the condition is true; if its value is 0 or missing, the condition is false.

0 = False
. = False
1 = True

As a result, you need to be careful when using the OR operator with a series of comparisons. Remember that only one comparison in a series of OR comparisons must be true to make a condition true, and any nonzero, nonmissing constant is always evaluated as true. Therefore, the following subsetting IF statement is always true:

 if B =2 or 3; 

SAS first evaluates B = 2, and the result can be either true or false. However, because the 3 is evaluated as nonzero and nonmissing (true), the entire expression is true. In the following statement, however, the condition is not necessarily true because either comparison can evaluate as true or false:

if B=2 or B=3;

-Urvish

Frequent Contributor
Posts: 77

Re: SAS query

Posted in reply to UrvishShah

try this code

data work.flag;

infile "C:\Documents and Settings\test\test.txt";

input name $ B;

if B = . then flag = 'Unknown';

else if B < 2 then flag = 'Low';

else if B < 4 then flag = 'Medium';

else flag = 'High';

PROC PRINT DATA = flag;

RUN;

Super User
Posts: 11,343

Re: SAS query

Posted in reply to TarunKumar

Or consider creating a custom format;

Proc format;

Value MyB

. = 'Unknown'

1= 'Low'

2,3 = 'Medium'

4 - high='High'

;

run;

<your data step to read the data>

Proc print data=work.flag;

     var name B;

     format B MyB. ;

run;

This has the flexibility that if multiple variables have the same coding you dont' need to create multiple Coded variables and if there comes a time you want to consider 1&2 as one group and 3 & 4 as another you make a different format and use that without creating another dataset and/or set of variables. Most analysis procedures can also use the formatted value for grouping;

proc freq data=work.flag;

     tables b;

     format b MyB. ;

run;

Super User
Posts: 19,772

Re: SAS query

Posted in reply to Siddharth123

Or

else B IN (2, 3) compared to B=2 or 3 (which is invalid).

B in (2,3) is also equivalent to B=2 or B=3

Ask a Question
Discussion stats
  • 4 replies
  • 279 views
  • 7 likes
  • 5 in conversation