BookmarkSubscribeRSS Feed
Haikuo
Onyx | Level 15

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;

Kurt_Bremser
Super User

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.

Haikuo
Onyx | Level 15

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.

Kurt_Bremser
Super User

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.

Haikuo
Onyx | Level 15

  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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 19 replies
  • 1956 views
  • 1 like
  • 11 in conversation