DATA Step, Macro, Functions and more

Create a new data set concatenating the observations

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 127
Accepted Solution

Create a new data set concatenating the observations

Dear Experts,

 

let's assume there is the following data set with one variable (country) and severa observations.

 

country

BU

JA

US

KR

 

How can I create a data set where I still have the variable but all the observations are compressed in one?

 

Expected output:

County

BU, JA, US,...,KR


Accepted Solutions
Solution
‎04-08-2016 10:37 AM
Super User
Super User
Posts: 7,970

Re: Create a new data set concatenating the observations

Posted in reply to Sir_Highbury

Hi,

 

You can use a retain cating each item onto one long varaible then output at the end:

data have;
  country="BU"; output;
  country="JA"; output;
  country="US"; output;
run;
data want (drop=country rename=(tmp=country));
  length tmp $2000;
  set have end=last;
  retain tmp;
  tmp=catx(",",tmp,country);
  if last then output;
run;

View solution in original post


All Replies
Solution
‎04-08-2016 10:37 AM
Super User
Super User
Posts: 7,970

Re: Create a new data set concatenating the observations

Posted in reply to Sir_Highbury

Hi,

 

You can use a retain cating each item onto one long varaible then output at the end:

data have;
  country="BU"; output;
  country="JA"; output;
  country="US"; output;
run;
data want (drop=country rename=(tmp=country));
  length tmp $2000;
  set have end=last;
  retain tmp;
  tmp=catx(",",tmp,country);
  if last then output;
run;
Frequent Contributor
Posts: 127

Re: Create a new data set concatenating the observations

Posted in reply to Sir_Highbury

many thanks

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 175 views
  • 1 like
  • 2 in conversation