Hello.
I want to make a variable to have multiple inputs.
The data and the desired results are as follows:
Data
| NAME | YEAR | Char |
| JOY | 2011 | AA |
| JOY | 2011 | BB |
| JOY | 2012 | AA |
| JOY | 2012 | BB |
| JOY | 2012 | CC |
Desired result
| NAME | YEAR | Find |
| JOY | 2011 | AA, BB |
| JOY | 2012 | AA, BB, CC |
If you know how, please help me.
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;
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
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;
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.