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

hi all ,

i saw someone try to flag ,from my presumtion on data step.

not the original code ,but i have tried to create ,what i saw.highlighted in yellow is what am specifically looking for.

proc sql noprint;

select count(*) into:cnt from sashelp.class;

quit;

data have;

set sashelp.class(keep=name sex age);

if _n_=&cnt then do;

a=1;

b='c';

end;

age_ind=age=age;

 

a_ind_1=a=a;

a_ind_2=a=.;

 

b_ind_1=b=b;

b_ind_2=b=" ";

run;

Question 1: why does age_ind=age=age flags all 1?

 

Question 2: why does a_ind_1=a=a flags all 1.(please see in xlsx attached)?(same as question 1)

Question 3: why does a_ind_2=a=. flags all 1 but the last( which is not missing) 0?

 

is it some TRUE/FALSE logic going on here??

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@himalayancat

is it some TRUE/FALSE logic going on here?

Yes, there is.

 

What code like age_ind=age=age; does:

1. Logical expression age=age  returns value of 1 if TRUE and 0 if False

2. Assigns value to variable age_ind  on the left.

 

....and because age=age is always TRUE age_ind will always be set to 1.

 

Unlike to other languages a single equal sign is used for both logical expressions and value assignments to variables so the code can look a bit confusing.

 

For clarity it would may be better to formulate such logic like:

age_ind =  (age EQ age);

 

 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

@himalayancat

is it some TRUE/FALSE logic going on here?

Yes, there is.

 

What code like age_ind=age=age; does:

1. Logical expression age=age  returns value of 1 if TRUE and 0 if False

2. Assigns value to variable age_ind  on the left.

 

....and because age=age is always TRUE age_ind will always be set to 1.

 

Unlike to other languages a single equal sign is used for both logical expressions and value assignments to variables so the code can look a bit confusing.

 

For clarity it would may be better to formulate such logic like:

age_ind =  (age EQ age);

 

 

ballardw
Super User

You might want to compare:

a_ind_2=a=.;

with

a_ind_2alt = missing(a);

 

and

b_ind_2=b=" ";

with

b_ind_2al= missing(b);

 

SAS by default returns 1 for "true" and 0 for "false" when evaluating and comparison on the right side of an assignment.

So for non-trivial, your a=a for example, this can be a slightly faster execution (and shorter code)than:

 

if a=. then a_ind_2=1;

else a_ind_2=0.

 

Something along those lines is very common when creating indicator variables or flags or other dichotomous results.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2927 views
  • 0 likes
  • 3 in conversation