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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 4070 views
  • 0 likes
  • 4 in conversation