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??
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);
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);
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.