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;
@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.
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.
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 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 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.
@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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.