Hi I have data like this:
Subjid visitdt kitno
201-001 2019-08-21 1001
201-001
201-001
201-001
210-001 2019-09-21 1003
I need to create another column called ecref and the result should look like this:
Subjid visitdt kitno ecref
201-001 2019-08-21 1001 1001
201-001 1001
201-001 1001
201-001 1001
210-001 2019-09-21 1003 1003
Thanks.
Please provide a meaningful title that describes the problem. A title like "Help with SAS code" applies to almost every problem.
Agree with the other poster that you have to provide a meaningful description of your problem. Look up the documentation for RETAIN. Then take care to reset ECREF to a missing value for each SUBJID. You do have your data already sorted correctly by SUBJID and another ID field like a record number or a sequence number within each subject, right? So something along the lines as below should work (UNTESTED).
data want ;
set have ;
by subjid ;
retain ecref ;
if first.subjid then ecref = . ;
if not missing(kitno) then ecref = kitno ;
run ;
Use the below code.
data want;
set have;
by Subjid;
retain ecref;
if kitno ne . then ecref = kitno;
run;
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.