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

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...

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Patrick_0-1647318027956.png

 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

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;

Patrick_0-1647318027956.png

 

AKuz
Barite | Level 11
Hi Patrick,

Thank you so much, it was so simple and worked perfectly!

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 2 replies
  • 1924 views
  • 2 likes
  • 2 in conversation