I am having trouble in my program using multiple created variables at once. I had created two new variables and when I go to use them, only the most recent one gets saved into my output. When I run an analysis, I get an error code that one does not exist, but it will let me run it if I only run that specific one.
data dap1; set epi.hpvd;
if CD4=. then cd=.;
if CD4 lt 500 and cd4 ge 0 then cd ='2';
if CD4 ge 500 then cd='1';
run;
data dap1; set epi.hpvd;
if hiv_load=. then h=.;
if hiv_load le 75 then h= '1';
if hiv_load gt 75 then h='2';
run;
proc univariate data=dap1;
class cd ;
run;
I am having trouble in my program using multiple created variables at once. I had created two new variables and when I go to use them, only the most recent one gets saved into my output. When I run an analysis, I get an error code that one does not exist, but it will let me run it if I only run that specific one.
data dap1; set epi.hpvd;
if CD4=. then cd=.;
if CD4 lt 500 and cd4 ge 0 then cd ='2';
if CD4 ge 500 then cd='1';
run;
data dap1; set epi.hpvd;
if hiv_load=. then h=.;
if hiv_load le 75 then h= '1';
if hiv_load gt 75 then h='2';
run;
proc univariate data=dap1;
class cd ;
run;
Your second DATA step overwrites the result dataset from the first. Combine both steps into one:
data dap1;
set epi.hpvd;
if 0 le CD4 lt 500
then cd = '2';
else if CD4 ge 500 then cd = '1';
if hiv_load = .
then h = ' ';
else if hiv_load le 75
then h = '1';
else h = '2';
run;
I've also removed one unnecessary IF, created the new variables as character, and optimized your code by using ELSE IF where appropriate.
In addition, you can make your coding and typing much easier — there's really no reason to put quotes around 1 and 2, when this would work just as well:
data dap1; set epi.hpvd;
if CD4=. then cd=.;
if CD4 lt 500 and cd4 ge 0 then cd =2;
if CD4 ge 500 then cd=1;
run;
-- Paige Miller
Register Today!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.