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

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 185 views
  • 0 likes
  • 4 in conversation