DATA Step, Macro, Functions and more

MISSING INDICATOR TRUE/FALSE

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

MISSING INDICATOR TRUE/FALSE

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??

 

 

 

 

 

 


Accepted Solutions
Solution
3 weeks ago
Respected Advisor
Posts: 4,736

Re: MISSING INDICATOR TRUE/FALSE

[ Edited ]
Posted in reply to himalayancat

@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


All Replies
Solution
3 weeks ago
Respected Advisor
Posts: 4,736

Re: MISSING INDICATOR TRUE/FALSE

[ Edited ]
Posted in reply to himalayancat

@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);

 

 

Super User
Posts: 13,508

Re: MISSING INDICATOR TRUE/FALSE

Posted in reply to himalayancat

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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