BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_Kh
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

 

A_Kh
Barite | Level 11
Hi Patrick,

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

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