If choose to use Hash:
data temp_1;
input subject $ arm $;
cards;
10000 NJ
10001 PA
10001 CA
10001 CA
10002 NA
10002 PA
10002 NA
10003 NJ
10004 NJ
;
data want;
declare hash h();
h.definekey('arm');
h.definedata('arm');
h.definedone();
declare hiter hi('h');
do until (last.subject);
set temp_1;
by subject notsorted;
length arm_list $ 100;
rc=h.add();
end;
do rc=hi.first() by 0 while (rc=0);
arm_list=catx(',',arm_list,arm);
rc=hi.next();
end;
drop arm rc;
run;
The sort nodupkey and data step solution;
data have;
infile cards;
length subject 5 arm $2;
input subject arm;
cards;
10001 PA
10001 CA
10001 CA
10002 NA
10002 PA
10002 NA
;
run;
proc sort data=have nodupkey; by subject arm; run;
data want (drop=arm rename=(arm_new=arm));
set have;
by subject;
length arm_new $ 20;
retain arm_new;
if first.subject then arm_new = '';
arm_new = catx(' + ',arm_new,arm);
if last.subject then output;
run;
Can't see what's long and ugly here.
Kurt, Similar solution (as well as the similar comments) has already been provided in the second answer (Nov 12, 2014 1:56 PM). I think we are too hash on the OP who is probably a SAS beginner.
The second post wouldn't really work, because arm_new was not included in the catx function. I basically delivered mine to rectify this small mistake. And to show that, as it often is with SAS, the simple data step solution is also the shortest and most easy to comprehend.
Well, here is the typical scenario of 'lost in translation', and I agree it is hard to follow every post in a long thread. when I say "second answer (Nov 12, 2014 1:56 PM). ", it is the second answer, not the second post. That is why I have attached time stamp as well.
The reason I want this to be straightened is that not everyone on this forum is experienced programmer, and the last thing we want is to have them confused.
Haikuo
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.