BookmarkSubscribeRSS Feed
saslearner14
Calcite | Level 5

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;
 

 

3 REPLIES 3
Kurt_Bremser
Super User

@saslearner14 wrote:

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.

Tom
Super User Tom
Super User

So you made dap1 with the new variable .

You then overwrote it with a different dataset that makes a different new variable.

 

If you want both variables in the same dataset that either make them both in the SAME data step.

Or use the output of the first step as the input of the second step.

PaigeMiller
Diamond | Level 26

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 381 views
  • 0 likes
  • 4 in conversation