DATA Step, Macro, Functions and more

Combining character variable into group of variables

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

Combining character variable into group of variables

I have a data set having a single column of names and 100 rows .

Is there way to combine names from every 10 rows into a string?

For example my data may be like this

aaaa

bbbb

abbc

ddda

....

...

What I want ot get is a new table with

aaaa bbbb abbbc

ddda efgh ijkl lmnop

.....

Is there a way to do it?


Accepted Solutions
Solution
4 weeks ago
Super User
Posts: 6,529

Re: Combining character variable into group of variables

Posted in reply to thesasuser

It's probably a bad idea that will only make your life harder down the road.  But it's not difficult:

 

data want;

length string $ 200;

do _n_=1 to 10 until (done);

   set have end=done;

   string = catx(' ', string, name);

end;

drop name;

run;

 

View solution in original post


All Replies
Super Contributor
Posts: 448

Re: Combining character variable into group of variables

Posted in reply to thesasuser

First group the data by 10 records each and then use proc transpose by the grouped variable.

 

DATA have(drop=i);
do i=1 to 200;
val=put(i,3.);
output;
end;
run;
data want;
retain group;
set have;
IF _N_=1 then group=1;
else if MOD(_N_-1,10)=0 then group+1;
run;

proc transpose data=want out=want1;
by group;
var val;
run;
Thanks,
Suryakiran
Solution
4 weeks ago
Super User
Posts: 6,529

Re: Combining character variable into group of variables

Posted in reply to thesasuser

It's probably a bad idea that will only make your life harder down the road.  But it's not difficult:

 

data want;

length string $ 200;

do _n_=1 to 10 until (done);

   set have end=done;

   string = catx(' ', string, name);

end;

drop name;

run;

 

Contributor
Posts: 38

Re: Combining character variable into group of variables

Posted in reply to thesasuser
Thanks
Both solutions are wonderful and good.
Accepted this as simpler code
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 101 views
  • 1 like
  • 3 in conversation