BookmarkSubscribeRSS Feed
Siddharth123
Obsidian | Level 7


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

4 REPLIES 4
UrvishShah
Fluorite | Level 6

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

TarunKumar
Pyrite | Level 9

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;

ballardw
Super User

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;

Reeza
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 860 views
  • 7 likes
  • 5 in conversation