BookmarkSubscribeRSS Feed
Hazelc
Calcite | Level 5

Hi,

I'm a beginner so please be kind! I need some help with the SAS code below, I am using two variables called calvingdate and dob to create a new variable called afc. However, I still need calvingdate to remain in my dataset (it disappears when I run the code below). Is there any command that will allow me to keep calvingdate while still creating the new variable afc? 🙂

 

proc sort data=together2;
by techid;
run;

data together2;
merge together2 (in=a) first_weight (in=b);
by techid;
if a and b;
run;

data together2;
set together2;
afc=calvingdate-dob;
run;

 

 

 

Thanks in advance!:)

 

3 REPLIES 3
Reeza
Super User
proc sort data=together2;
by techid;
run;

data together3;
merge together2 (in=a) first_weight (in=b);
by techid;
if a and b;
run;

data together4;
set together3;
afc=calvingdate-dob;
run;

 

There's nothing in your code to indicate why that variable may be dropped. I modified your code, you really shouldn't be using the same input and output data set names when you're coding, it makes it really hard to find errors. Try the code above and if it still doesn't work, please post the code and your log. There's likely something there that indicates why this is happening.

 


@Hazelc wrote:

Hi,

I'm a beginner so please be kind! I need some help with the SAS code below, I am using two variables called calvingdate and dob to create a new variable called afc. However, I still need calvingdate to remain in my dataset (it disappears when I run the code below). Is there any command that will allow me to keep calvingdate while still creating the new variable afc? 🙂

 

proc sort data=together2;by techid;run;

data together2;merge together2 (in=a) first_weight (in=b);by techid;if a and b;run;

data together2;set together2;afc=calvingdate-dob;run;

 

Thanks in advance!:)

 


 

Reeza
Super User
Please note that I edited your post slightly for formatting of the code and text and renamed it. Descriptive subject lines help other users find the posts when they have similar issues.
ballardw
Super User

@Hazelc wrote:

Hi,

I'm a beginner so please be kind! I need some help with the SAS code below, I am using two variables called calvingdate and dob to create a new variable called afc. However, I still need calvingdate to remain in my dataset (it disappears when I run the code below). Is there any command that will allow me to keep calvingdate while still creating the new variable afc? 🙂

 

proc sort data=together2;
by techid;
run;

data together2;
merge together2 (in=a) first_weight (in=b);
by techid;
if a and b;
run;

data together2;
set together2;
afc=calvingdate-dob;
run;

 

 

 

Thanks in advance!:)

 


Is the VARIABLE gone or are the values missing? And which data set(s) contain the variables calvingdate and dob?

IF first_weight has a missing value for calvingdate (or any other variable common with together2) then the value from first_weight replaced the value from together2. That is the way merge works.

 

Since your data step after the sort replaced together2 with fewer records you will have to go back to a previous step and recreate the original together2.

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
  • 558 views
  • 2 likes
  • 3 in conversation