I have been using this code to create a new group variable, and someone said this code won't work. The code should be
if (Systolic_BP0 >= 120 and Systolic_BP0 < 130) then BP_group = 1;
Is this true? Thank you!
You have been given poor advice.
Perhaps they are used to programming in other languages that do not support the abbreviated syntax of
expression operator expression operator expression ....
that SAS supports.
The documentation calls it an implied AND.
Two comparisons with a common variable linked by AND can be condensed with an implied AND. For example, the following two subsetting IF statements produce the same result:
if 16<=age and age<=65;
if 16<=age<=65;
To see what it means use it in a WHERE statement
data have;
input Systolic_BP0 @@;
cards;
100 120 121 129 130 131 .
;
data want;
set have;
where 120 <= Systolic_BP0 < 130 ;
run;
and then check the SAS log for the note that shows what SAS transformed it into.
NOTE: There were 3 observations read from the data set WORK.HAVE. WHERE (Systolic_BP0>=120 and Systolic_BP0<130); NOTE: The data set WORK.WANT has 3 observations and 1 variables.
Note you are not limited to just a single "implied AND" in an expression.
54 data want; 55 set sashelp.class; 56 where age < weight < height > 5; 57 run; NOTE: There were 1 observations read from the data set SASHELP.CLASS. WHERE (age<weight) and (weight<height) and (height>5); NOTE: The data set WORK.WANT has 1 observations and 5 variables.
You have been given poor advice.
Perhaps they are used to programming in other languages that do not support the abbreviated syntax of
expression operator expression operator expression ....
that SAS supports.
The documentation calls it an implied AND.
Two comparisons with a common variable linked by AND can be condensed with an implied AND. For example, the following two subsetting IF statements produce the same result:
if 16<=age and age<=65;
if 16<=age<=65;
To see what it means use it in a WHERE statement
data have;
input Systolic_BP0 @@;
cards;
100 120 121 129 130 131 .
;
data want;
set have;
where 120 <= Systolic_BP0 < 130 ;
run;
and then check the SAS log for the note that shows what SAS transformed it into.
NOTE: There were 3 observations read from the data set WORK.HAVE. WHERE (Systolic_BP0>=120 and Systolic_BP0<130); NOTE: The data set WORK.WANT has 3 observations and 1 variables.
Note you are not limited to just a single "implied AND" in an expression.
54 data want; 55 set sashelp.class; 56 where age < weight < height > 5; 57 run; NOTE: There were 1 observations read from the data set SASHELP.CLASS. WHERE (age<weight) and (weight<height) and (height>5); NOTE: The data set WORK.WANT has 1 observations and 5 variables.
If bp_group is to be a Boolean value (yes/no), you can shorten it to
bp_group = (120 <= Systolic_BP0 < 130);
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.