BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
nxmogil
Obsidian | Level 7

Hi All,

 

I need to manually input date of births to ids like this for nearly 20 ids. 

Is there any alternative way to simplify this code

 

Data test;

set old;

if id = "02" then do;

dob = input ('1948 - 03- 21', yymmdd10.) ;

end;

if id = "03" then do;

dob = input ('1954- 02- 25', yymmdd10.) ;

end;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@nxmogil wrote:
Hi Great thanks once again,

in my old data set i have id gender, addm phone and many variables including this DOB. So in my trans dataset according to you can there be onlt two variables (ID, Dob) and can do update still

Yes.  The transaction dataset only needs the KEY variable(s) and whatever variables you want to make changes to.  

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

If there is no pattern, then you are stuck typing each dob.

 

You could reduce the amount of typing

 

if id = "02" then dob = '21MAR1948'd;

 

You could also read the ID and DOB into SAS from a CARDS/DATALINES statement, or read them in from an external file, which would probably be even less typing.

--
Paige Miller
nxmogil
Obsidian | Level 7
Hi ,
Great thanks for the quick response. Actually i am updating a dataset for which few ids have DOB and for few of them missing. Thats the reason I am updationg DOBs for missing ids.
Tom
Super User Tom
Super User

So you want to apply TRANSACTIONS to and existing dataset?

 

That is what the UPDATE statement in SAS is designed for.

 

So first make your transaction dataset that has the KEY variable(s) and any values you want modified.

data trans;
  length id $2 dob 8;
  input id dob :yymmdd.;
  format dob yymmdd10.;
cards;
02 1948-03-21
03 1954-02-25
;

Then make sure both the OLD and the TRANS datasets are sorted by the KEY variable(s).

 

Now you can use the UPDATE statement to apply the transactions to OLD and make a NEW dataset.

data new;
  update old trans;
  by id;
run;

If you have multiple variables that have transactions you can include those also.  Just put missing values for the variables that you don't want the transaction to change. (If you want to force a value to missing that is an advanced topic.)

data trans;
  length id $2 dob 8 gender $1;
  input id dob :yymmdd. gender;
  format dob yymmdd10.;
cards;
02 1948-03-21 .
03 1954-02-25 M
05 . F
;
nxmogil
Obsidian | Level 7
Hi Great thanks once again,

in my old data set i have id gender, addm phone and many variables including this DOB. So in my trans dataset according to you can there be onlt two variables (ID, Dob) and can do update still
Tom
Super User Tom
Super User

@nxmogil wrote:
Hi Great thanks once again,

in my old data set i have id gender, addm phone and many variables including this DOB. So in my trans dataset according to you can there be onlt two variables (ID, Dob) and can do update still

Yes.  The transaction dataset only needs the KEY variable(s) and whatever variables you want to make changes to.  

nxmogil
Obsidian | Level 7
Thank you so much it worked for me
Tom
Super User Tom
Super User

@nxmogil wrote:
Hi Great thanks once again,

in my old data set i have id gender, addm phone and many variables including this DOB. So in my trans dataset according to you can there be onlt two variables (ID, Dob) and can do update still

We used to use NAMED INPUT mode to create the transaction datasets.  It is a little more typing per record, but it is clearer when looking at the data lines which variables each transaction is impacting.

 

You can use the IF 0 THEN SET trick to get the variables defined based on the definition in the OLD dataset.

data trans;
  if 0 then set old;
  input id gender= addm= phone= dob= ;
cards;
01 dob=2001-01-05
02 gender=M
;

Also note that the KEY variables need to uniquely identify the observations in the OLD dataset.  But you can have multiple transaction observations per KEY value.  They will all be applied in order so that the end result is that the NEW dataset also has the property that the KEY variables uniquely identify the observations.

 

 

PaigeMiller
Diamond | Level 26

@nxmogil wrote:
Hi ,
Great thanks for the quick response. Actually i am updating a dataset for which few ids have DOB and for few of them missing. Thats the reason I am updationg DOBs for missing ids.

Hello @nxmogil , its always helpful if you explain the reasons you are doing something in your original message. You will get faster and better answers than simply posting code without the background reasons why you are doing something.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1292 views
  • 0 likes
  • 3 in conversation