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!!!!!
See Example 3 here: https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lestmtsref/p09213s9jc2t99n1vx0omk2rh9ps.htm
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;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: