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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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);

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26
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?

--
Paige Miller
pramenon1
Obsidian | Level 7
Hi, sorry for the confusion.
-Basically if any of the four variables even has one value less than 200 lowcd4 should = 1.
-If all values are missing or all values are 200 or above (or some combination of this) than lowcd4 = 0.

Please let me know if that makes more sense!
Tom
Super User Tom
Super User

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;

 

 

FreelanceReinh
Jade | Level 19

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);
PaigeMiller
Diamond | Level 26

@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!

--
Paige Miller
FreelanceReinh
Jade | Level 19

@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: 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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 4833 views
  • 5 likes
  • 5 in conversation