I am trying to do something along the lines of if Varibles A, B, C, or D contain values less then 200 then Y (new variable) = 1, if none of them contain values or if all their values are above 200 then Y = 0. What I have right now is :
Data dtanl.hivcancerdta;
set dtanl.hivcancerdta;
IF cd4_cnt1 < . THEN lowcd4 = 0;
IF cd4_cnt2 < . THEN lowcd4 = 0;
IF cd4 cnt3 < . THEN lowcd4 = 0;
IF cd4_cnt4 < . THEN lowcd4 = 0;
IF cd4_cnt1 < 200 THEN lowcd4 = 1;
IF cd4_cnt2 < 200 THEN lowcd4 = 1;
IF cd4 cnt3 < 200 THEN lowcd4 = 1;
IF cd4_cnt4 < 200 THEN lowcd4 = 1;
ELSE lowcd4 = 0;
RUN;
Its ugly and isn't working. Is there a better way to do this. I tried looking online and some options use do statements but I dont have much experience with it. Any help would be appreciated.
Hello @pramenon1,
If your real condition for LOWCD4=1 is that any of the four CD4_CNT variables has a nonmissing value <200 (and LOWCD4=0 otherwise) I would use
lowcd4=(min(of cd4_cnt1-cd4_cnt4,200)<200);
data dtanl.hivcancerdta1;
set dtanl.hivcancerdta;
if whichn(.,of cd4_cnt:)>0 then lowcd4=0;
else if cd4_cnt1<200 or cd4_cnt2<200 or cd4_cnt3<200 or cd4_cnt4<200 then lowcd4=1;
else lowcd4=0;
run;
You requirements don't seem to be complete. What happens if one of the variables is missing and one of the variables is <200 and one of the variables is >=200?
So find the minimum value. Then test if it is less that 200. You can test if the value is between .Z and 200 to exclude missing values (which are less than any actual number).
Then just assign the reulst of the test to your new variable since SAS evaluates boolean expression as 1 or 0. No need for IF/THEN.
lowcd4 = .Z < min(of cd4_cnt1-cd4_cnt4) < 200;
Hello @pramenon1,
If your real condition for LOWCD4=1 is that any of the four CD4_CNT variables has a nonmissing value <200 (and LOWCD4=0 otherwise) I would use
lowcd4=(min(of cd4_cnt1-cd4_cnt4,200)<200);
@FreelanceReinh wrote:
Hello @pramenon1,
If your real condition for LOWCD4=1 is that any of the four CD4_CNT variables has a nonmissing value <200 (and LOWCD4=0 otherwise) I would use
lowcd4=(min(of cd4_cnt1-cd4_cnt4,200)<200);
A much better solution than mine!
IF cd4_cnt1 < . THEN lowcd4 = 0;
Since MISSING is considered the lowest value by definition, this condition can NEVER be true.
@Kurt_Bremser wrote:
IF cd4_cnt1 < . THEN lowcd4 = 0;
Since MISSING is considered the lowest value by definition, this condition can NEVER be true.
Almost never, to be more precise, as . is only the second lowest numeric value.
data test;
cd4_cnt1=._;
if cd4_cnt1 < . then lowcd4 = 0;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.