DATA Step, Macro, Functions and more

concatenating all observations in single

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 85
Accepted Solution

concatenating all observations in single

Hi All,

Following is my dataset

data have;

input var1 $20;

cards;

robert

is

my

boss

;

run;

Now , i want to have one single macro variable which would resolve to robert is my boss

In short, I want to concatenate all obseervations in one single macro. Catch is not to use Proc sql. I know below single statement would have solved my purpose, but I have to use datastep and not proc sql.

proc sql; select var1 into :macvar1 separated by " " from have;


Any help would be highly appreciated


Accepted Solutions
Solution
‎06-05-2013 05:20 AM
Regular Contributor
Posts: 151

Re: concatenating all observations in single

I had the same idea as .  Here is the code I wrote

data _null_;

set have end=last;

length result $200;

retain result;

call catx(" ",result,var1);

if last then call symput('macvar1',result);

run;

%put &macvar1.;

View solution in original post


All Replies
Super User
Posts: 5,256

Re: concatenating all observations in single

Why not SQL?

One should chose whatever tool seems best/easiest for each task...

Anyway, concatenate your vaues into a retained value, and the call symput when the data step reaches the last record/EOF (I guess that your real life data is not provided in datalines)

Data never sleeps
Frequent Contributor
Posts: 85

Re: concatenating all observations in single

Agree Linus, but that's what my client's requirement is. They are insisting on data steps as they previously had experienced that having mixed macro variables (few from datastep and few from proc sql) tend to create problems in their environment. Anyways, can you please elaborate on your approach (EOF and retain). I tried using that as well , however it could not work.

Solution
‎06-05-2013 05:20 AM
Regular Contributor
Posts: 151

Re: concatenating all observations in single

I had the same idea as .  Here is the code I wrote

data _null_;

set have end=last;

length result $200;

retain result;

call catx(" ",result,var1);

if last then call symput('macvar1',result);

run;

%put &macvar1.;

Frequent Contributor
Posts: 85

Re: concatenating all observations in single

Thanks keith... that worked .. I had used retain earlier in sum but never thought it could be applied to char variables as well

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 347 views
  • 2 likes
  • 3 in conversation