Help using Base SAS procedures

how to merge variables

Posts: 40

how to merge variables

I have data something like this -


id1   abc

id1   def

id2   pqr

id2   rst


------   etc



I want to have like this in output dataset -


id1  abc/def

id2   pqr/rst



thanks in advance,

Trusted Advisor
Posts: 1,256

Re: how to merge variables

[ Edited ]

Try this:

data have;
input id $ text $;
id1   abc
id1   def
id2   pqr
id2   rst

data want;
length alltext $30; /* Please adapt the length 30 to your needs! */
do until(;
  set have;
  by id;
  alltext=catx('/', alltext, text);
drop text;

proc print data=want;
var id alltext;

If you want to have ID as the first variable in the dataset (and not only in the output), you can move the LENGTH statement after the SET statement, immediately before the assignment alltext=....

Super User
Posts: 23,771

Re: how to merge variables

And if you don't understand the do loop before the SET statement here's another, old fashioned method. @FreelanceReinhard solution is probably more efficient.


data want;
set have;
by id;

length var $100.;
retain var;

if then call missing(var);
var=catx('/', var, text);

if then output;

proc print;run;
Ask a Question
Discussion stats
  • 2 replies
  • 1 like
  • 3 in conversation