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

Hello

I have the following question.

In each month I get a data set that is sent to me (I don't create it).

The structure of the table is not fixed and there are different columns every month.

For example:

In month June 2020 there are 3 columns of revenue called: Revenue1,Revenue2,Revenue3

In month May 2020 there are 2 columns of revenue called: Revenue1,Revenue2

In month April 2020 there are 2 columns of revenue called: Revenue1,Revenue2,Revenue3,Revenue4

 

In every month after getting the data set I need to perform manipulation on the data set.

Let's say that I want to concatenate all variables that have following name "Revenue"(with comma between values)

What is the way to concatenate the columns with name Revenue when I don't know in advance how many columns with these name  have.

I am looking for a dynamic code that concatenate fields when number of fields is not fixed.

 

Data t2004;
input Id Revenue1 Revenue2 Revenue3 Revenue4;
cards;
1 10 20 30 40
2 15 30 45 60
3 5 10 15 20
;
Run;

Data t2204_new;
set t2004;
Revenue_new=CATX(',',Revenue1,Revenue2,Revenue3,Revenue4);
/*need to find way to do it without knowing number of columns before*/
Run;

 

1 ACCEPTED SOLUTION
4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

you may try

Revenue_new=CATX(',', of Revenue1--Revenue4);

Thanks,
Jag
Ronein
Meteorite | Level 14

It is not good because you don't know in advance that there are 4 Revenue columns.

This is exactly the issue.

 

 

Jagadishkatam
Amethyst | Level 16

ok then try this

 

Data t2204_new;
length Revenue_new $200.;
set t2004;
Revenue_new=CATX(',',of Revenue:);

Run;
Thanks,
Jag

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 4 replies
  • 929 views
  • 2 likes
  • 3 in conversation