- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
-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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
IF cd4_cnt1 < . THEN lowcd4 = 0;
Since MISSING is considered the lowest value by definition, this condition can NEVER be true.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;