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

I have data in one format and need to get it to another (nonnormalized) format for manipulation/display purposes.

 

data have;
input mycode mytype $1.;
datalines;
111 a
111 b
111 c
222 a
222 b
222 d
222 e
222 f
222 g
333 a
333 q
444 s
555 s
555 e
555 t
666 a
666 b
;
run;

The desired yucky format is 

 

111 a b c
222 a b d e f g
333 a q
444 s
555 s e t
666 a b

I know this can be done, but I don't know how to do it. 

 

Essentially I think I want to sort by mycode and then do a loop that is:

      for each value of mycode until the last.mycode in each group make mynewtype = mynewtype concatenate mytype on the end

 

but obviously I don't know how to translate that into working code.

 

I don't care whether 

222 a b d e f g

comes out as 7 columns or 2 columns with "a b d e f g" as the value in the second column. 

 

Could somebody please point me in the correct direction?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

data want; length newx $20.; set have; by mycode; retain newx; if first.mycode then newx=''; if mytype ne '' then newx=catx(' ',newx,mytype); if last.mycode; drop mytype; run;

 

Thanks,
Jag

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

data want; length newx $20.; set have; by mycode; retain newx; if first.mycode then newx=''; if mytype ne '' then newx=catx(' ',newx,mytype); if last.mycode; drop mytype; run;

 

Thanks,
Jag
HB
Barite | Level 11 HB
Barite | Level 11

That results in 

 

newx	mycode
a b c	111
a b d e f g	222
a q	333
s	444
s e t	555
a b	666

which will work just fine.

 

World record response time!!

 

Thank you. 

ScottBass
Rhodochrosite | Level 12

I don't care whether 

222 a b d e f g

comes out as 7 columns or 2 columns with "a b d e f g" as the value in the second column. 

 

Another approach is to use PROC TRANSPOSE, where you can have "the best of both worlds":

 

data have;
input mycode mytype $1.;
datalines;
111 a
111 b
111 c
222 a
222 b
222 d
222 e
222 f
222 g
333 a
333 q
444 s
555 s
555 e
555 t
666 a
666 b
;
run;

proc transpose data=have out=want (drop=_name_);
   by mycode;
   var mytype;
run;

data vwant / view=vwant;
   set want;
   length mytype $100;
   mytype=catx(" ",of col:);
   keep mycode mytype;
run;

HTH...

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1642 views
  • 3 likes
  • 3 in conversation