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.

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!

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