BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
J_Park
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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.
J_Park
Obsidian | Level 7
Thank you so much for confirming that the code I have been using is correct!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 121 views
  • 0 likes
  • 2 in conversation