BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Suminder
Calcite | Level 5

Hi Guys,

 

I have following datasets

dataset1

nameid      name         sportsPlayed       howGood

 1               sam           cricket                   vg

 1               sam           football                    g

 1               sam           badminton               a

2                ravi            cricket                     vg

2                ravi            chess                      vg

 

The output sas table has to be

outputDATASET

nameid       name      sportsPlayed

1                  sam       cricket|football|badminton

2                  ravi        cricket|chess

 

I tried using sort and 'by' but seems like I am not getting the idea correctly...

 

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Assuming that NAME is just a decode of NAMEID you could do something like this:

data want ;
   set have ;
   by nameid ;
   length list_of_SportsPlayed $200 ;
   if first.nameid then list_of_SportsPlayed = SportsPlayed ;
   else list_of_SportsPlayed =catx('|',list_of_SportsPlayed ,SportsPlayed);
   retain list_of_SportsPlayed ;
   if last.nameid;
   drop SportsPlayed HowGood;
run;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Assuming that NAME is just a decode of NAMEID you could do something like this:

data want ;
   set have ;
   by nameid ;
   length list_of_SportsPlayed $200 ;
   if first.nameid then list_of_SportsPlayed = SportsPlayed ;
   else list_of_SportsPlayed =catx('|',list_of_SportsPlayed ,SportsPlayed);
   retain list_of_SportsPlayed ;
   if last.nameid;
   drop SportsPlayed HowGood;
run;
Suminder
Calcite | Level 5

hi Tom,

 

Thanks for the quick reply!

 

It is returning 5 observations whereas required is just two observations....pls have another look...

Tom
Super User Tom
Super User

You forgot to only keep the last observation per group.

PeterClemmensen
Tourmaline | Level 20
data have;
input nameid name $ sportsPlayed :$10. howGood $;
datalines;
1 sam cricket vg
1 sam football g
1 sam badminton a
2 ravi cricket vg
2 ravi chess vg
;

data want(drop=_: howgood);
   do until (last.nameid);
      set have(rename=sportsPlayed=_sportsPlayed);
      by nameid;
      length sportsPlayed $ 200;
      sportsPlayed=catx('|', sportsPlayed, _sportsPlayed);
   end;
run;
ballardw
Super User

If you like your final Sportsplayed list to possibly have the values appear in a semi-consistent order:

data have;
input nameid name $ sportsPlayed :$10. howGood $;
datalines;
1 sam cricket vg
1 sam football g
1 sam badminton a
2 ravi cricket vg
2 ravi chess vg
;

proc transpose data= have out=trans (drop=_name_);
by nameid name;
var sportsplayed;
run;

data want;
   set trans;
   array c col: ;
   call sortc (of c(*));
   sportsplayed = catx('|',of c(*));
   keep nameid name sportsplayed;
run;

This would have two (or more people) with chess and cricket all have the same "chess|cricket" where processing in order might have some with "chess|cricket" and others with "cricket|chess".

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 5 replies
  • 672 views
  • 0 likes
  • 4 in conversation