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

Hello.

I want to make a variable to have multiple inputs.

The data and the desired results are as follows:

 

Data

 

NAMEYEARChar
JOY2011AA
JOY2011BB
JOY2012AA
JOY2012BB
JOY2012CC

 

Desired result

NAMEYEARFind
JOY2011AA, BB
JOY2012AA, BB, CC

 

If you know how, please help me.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input NAME $ YEAR	Char $;
cards;
JOY	2011	AA
JOY	2011	BB
JOY	2012	AA
JOY	2012	BB
JOY	2012	CC
;

data want;
 do until(last.year);
  set have;
  by name year;
  length want $ 800;
  want=catx(',',want,char);
 end;
 drop char;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20
data have;
input NAME $ YEAR Char $;
datalines;
JOY 2011 AA
JOY 2011 BB
JOY 2012 AA
JOY 2012 BB
JOY 2012 CC
;

data want(drop=Char);
    do until (last.year);
        set have;
        length Find $100;
        by year;
        Find=catx(', ', Find, Char);
    end;
run;

 

 

Result:

 

 

Name Year   Find
JOY  2011   AA, BB
JOY  2012   AA, BB, CC

 

Ksharp
Super User
data have;
infile cards expandtabs truncover;
input NAME $ YEAR	Char $;
cards;
JOY	2011	AA
JOY	2011	BB
JOY	2012	AA
JOY	2012	BB
JOY	2012	CC
;

data want;
 do until(last.year);
  set have;
  by name year;
  length want $ 800;
  want=catx(',',want,char);
 end;
 drop char;
run;
Kurt_Bremser
Super User

Use by-group processing and a retained variable:

data have;
input NAME $ YEAR Char $;
datalines;
JOY 2011 AA
JOY 2011 BB
JOY 2012 AA
JOY 2012 BB
JOY 2012 CC
;

data want;
set have;
by name year;
length chars $20;
retain chars;
if first.year
then chars = char;
else chars = catx(',',chars,char);
if last.year;
drop char;
run;

proc print data=want noobs;
run;

Result:

NAME	YEAR	chars
JOY	2011	AA,BB
JOY	2012	AA,BB,CC
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
  • 4021 views
  • 0 likes
  • 4 in conversation