DATA Step, Macro, Functions and more

Can a macro be used to concatenate variables into a new variable?

Reply
N/A
Posts: 0

Can a macro be used to concatenate variables into a new variable?

Hi- I'm wondering if a macro can be written that concatenates comments into one comment field. For example, the code below concatenates 8 comment fields. However, the maximum number of comments can change each time it is run, so I want it to be flexible so that if the maximum number of comments changes from 8 to 10, it will concatenate all 10 comment fields. Any help would be greatly appreciated!

data dat2 (keep=compyr fnlcomment);
set wide;
fnlcomment=cmt1||' '||cmt2||' '||cmt3||' '||cmt4||' '||cmt5||' '||cmt6||' '||cmt7||' '||cmt8;
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: Can a macro be used to concatenate variables into a new variable?

The code below demonstrates one string concatenation technique when the variables have a stem value:

data _null_;
retain cmnt1-cmnt5 'xyz';
length fullcmt $100;
fullcmt = catx(' ',of cmnt: );
putlog _all_;
run;

Another approach when a stem variable prefix is not available, consider using an ARRAY and a DO I=1 TO DIM(); END; to concatenate each individual variable, referenced as an array component.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 1 reply
  • 106 views
  • 0 likes
  • 2 in conversation