Hi Community,
I would appreciate if someone could share her/his experience in resolving following problem;
In a given dataset there is a character variable with different values in each row (multiple rows). After transposing all records vertically, i need to concatenate all values into a single variable, separated by a comma. As variable numbers differ based on the row numbers, I'm unable to build a programming logic using loop or macro.
eg.
have:
var1 var2 var3 var4;
a b c d
need:
var1
a,b,c,d
I used macro var in summing these similar numeric variables, like ;
data need;
set have;
var1= sum(of var1-var&num);
run;
Now for character variable, trying to use concatenation, like;
data need;
set have;
var1= catx(",", var1, var&num);... but it does not cover multiple variables...
If your variables follow some naming pattern - something you can chose if using Proc Transpose - then below code should work for both character and numeric variables.
data have;
infile datalines dlm='|' dsd truncover;
input (var_1-var_4) ($);
datalines;
a|b|c|d
1||3
;
data want;
set have;
length new_var $20;
new_var=catx(',',of var_:);
run;
If your variables follow some naming pattern - something you can chose if using Proc Transpose - then below code should work for both character and numeric variables.
data have;
infile datalines dlm='|' dsd truncover;
input (var_1-var_4) ($);
datalines;
a|b|c|d
1||3
;
data want;
set have;
length new_var $20;
new_var=catx(',',of var_:);
run;
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 save with the early bird rate—just $795!
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.