Hi I have data look like this.
I want like this.
so basically want to combine drug in one variable for each unique id and seq.
Any pointer please ?
Thanks
Hi @dpa Sure. The logic is pretty straight forward technique that does row-wise concatentation for each by group. In your case each by-group is a unique combination of Id Seq. Now at this point you'd have already guessed the do until(last.seq) continues to process/execute up until all the records of each by group is read and processed. The concatentation takes place for each record and every record of a by group for the variable drug and finallyy written to the output dataset once the processing is complete. The same is repeated for every such By-group until all records in the input dataset are read and processed. Hope this helps?
Something like below should do. Data "have" must be sorted by ID and SEQ.
data want;
set have;
by id seq;
length drug_list $1000;
drug_list=catx(',',drug_list,drug);
if last.seq then
do;
output;
call missing(drug_list);
end;
drop drug;
run;
@dpa wrote:
sorry thanks but didn't work
Just for the future: It helps all of us if you let us know WHAT didn't work - like some description and/or post the relevant log section which shows the issue.
Forgot to add a Retain statement so variable drug_list doesn't get re-initialized for every iteration of the data step. Below code that should be o.k.
data want;
set have;
by id seq;
length drug_list $1000;
retain drug_list;
drug_list=catx(',',drug_list,drug);
if last.seq then
do;
output;
call missing(drug_list);
end;
drop drug;
run;
Just to be consistent about asking, what will you be able to do the combined drug variable that you cannot do with the data in current form?
Almost any any analysis is going to be much harder with multiple values in a single variable.
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.