BookmarkSubscribeRSS Feed
Julie99999
Obsidian | Level 7
data Heart / view=Heart;
set sashelp.heart;
select (Smoking_Status);
   when ('Non-smoker')        Smoking_Cat=1;
   when ('Light (1-5)')       Smoking_Cat=2;
   when ('Moderate (6-15)')   Smoking_Cat=3;
   when ('Heavy (16-25)')     Smoking_Cat=4;
   when ('Very Heavy (> 25)') Smoking_Cat=5;
   otherwise                  Smoking_Cat=.;
end;
run;


Hi all! I have a question on the code above using select().
Is there anyway to select two variables for the condition?
what if I want to select Smoking_Status = "Heavy" and Age > 30, is there way to use select() when in this case for multiple conditions?

TIA!!!!!


 

3 REPLIES 3
mkeintz
PROC Star

You want to use a compound condition in your when statements.  Generally, you can only do this if the SELECT statement has no argument, as in;

 

select ;
  when (age>30 and smoking_status='Heavy') smoking_cat=1;
  when (.......); smoking_cat=...;
  otherwise ....;
end;

Or else you could nest SELECT groups, as in;

 

  if age^=. then ageover30=(age>30);
  select (ageover30);
    when (1) select (smoking_status);
      when ('Non-smoker')        Smoking_Cat=1;
      when ('Light (1-5)')       Smoking_Cat=2;
      when ('Moderate (6-15)')   Smoking_Cat=3;
      when ('Heavy (16-25)')     Smoking_Cat=4;
      when ('Very Heavy (> 25)') Smoking_Cat=5;
      otherwise                  Smoking_Cat=.;
    end;
    when (0) select (smoking_status);
      when ('Non-smoker')        Smoking_Cat=11;
      when ('Light (1-5)')       Smoking_Cat=12;
      when ('Moderate (6-15)')   Smoking_Cat=13;
      when ('Heavy (16-25)')     Smoking_Cat=14;
      when ('Very Heavy (> 25)') Smoking_Cat=15;
      otherwise                  Smoking_Cat=.;
    end;
    otherwise smoking_cat=.;
  end;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
FreelanceReinh
Jade | Level 19

Hi @Julie99999,

 

You can also create your own "compound expression." Similar to mkeintz's example:

data want;
set sashelp.heart(rename=(AgeAtStart=age));
if age>.z then /* redundant for SASHELP.HEART */
select (catx('|', age>30, char(smoking_status,1)));
  when ('1|N') Smoking_Cat=1;
  when ('1|L') Smoking_Cat=2;
  when ('1|M') Smoking_Cat=3;
  when ('1|H') Smoking_Cat=4;
  when ('1|V') Smoking_Cat=5;
  when ('0|N') Smoking_Cat=11;
  when ('0|L') Smoking_Cat=12;
  when ('0|M') Smoking_Cat=13;
  when ('0|H') Smoking_Cat=14;
  when ('0|V') Smoking_Cat=15;
  otherwise    Smoking_Cat=.;
end;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 917 views
  • 3 likes
  • 4 in conversation