Hello
I have an input table that have one variable called YYMM (Type:char).
This table has only 3 rows with following data:
1807
1806
1712
Task1- Create a new variable called NewVar with following values (type: char):
1807
1807+1806
1807+1806+1712
Thanks
Joe
Task2- Take the last value from NewVar column and create a sas macro varaible called vector
%put &vector will give is value 1807+1806+1712
Data inputTbl;
Input YYMM;
1807
1806
1712
;
Run;
Do like this
data inputTbl;
Input YYMM $;
datalines;
1807
1806
1712
;
Run;
data want;
set inputTbl end=eof;
length NewVar $100;
NewVar=catx('+', NewVar, YYMM);
retain NewVar;
if eof then call symputx('vector', NewVar);
run;
%put &vector.;
Retain the new variable (set a sufficient length), use catx to concatenate the values, and call symput when you reach the end of the dataset (use end= option in the dataset).
Edit: what @PeterClemmensen said 😉
Is this just for the sake of building skills? Because you can skip the DATA step and go directly to creating the macro variable:
proc sql;
select MMYY into : vector separated by '+' from have;
quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.