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

 

Hello all,

 

Not sure why but I am unable to automate this portion:

 

%macro title();

%let allCodes = %sysfunc(CATX(",", &Codes1, &Codes2, &Codes3, &Codes4 ));

%put &allCodes;

%mend;

 

%title();

 

 

I am doing this below for automating the above %let allCodes statement - but seeing errors:

 

%macro attempt1();

%do i = 1 %TO 3;

%let allCodes = %sysfunc(CATX(",", &Codes&i.

%end;

&Codes4 ));

%put &allCodes;

%mend;

attempt1();

 

 

Just trying to automate the macro title() using DO loop but it gives errors...any ideas would be highly appreciated.

 

Regards,

 

Tina

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
Diamond | Level 26

When you run this

%let string1 = I like;
%let string2 = Kermit;
%let string3 = the;
%let string4 = Frog;

%put &=string1 &=string2 &=string3 &=string4;


%macro attempt ;
%global allcodes allcodes2;
%let allCodes =;
%do i = 1 %TO 4;

%let allCodes = &allCodes &&string&i;

%if &i = 1 %then %let allcodes2 = (&&string&i;
%else %let allcodes2 = &allcodes2 &&string&i;

%end;
%let allcodes2 = &allcodes2)%str(;);


%put &=allCodes &=allcodes2;

%mend attempt;

%attempt

Then &allcodes should have all 4 strings and allcodes2 should have parens and a semi-colon.

 

Cynthia

View solution in original post

5 REPLIES 5
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

don't you need to array codes1 through codes3 before you us the do I = 1 to 3?

Cynthia_sas
Diamond | Level 26

Hi:

  There is no need to use %SYSFUNC or the CATX function. Consider this:

%let string1 = I like;
%let string2 = Kermit;
%let string3 = the;
%let string4 = Frog;
%let bigstring = &string1 &string2 &string3 &string4;

%put &=string1 &=string2 &=string3 &=string4;
%put &=bigstring;

and if I wanted to make BIGSTRING2 with a different character between the values, I could do this:


%let bigstring2 = &string1~&string2#&string3^&string4;

%put &=bigstring2;

CATX and SYSFUNC are not needed in what you describe.

 

Cynthia

rosegarden81
Obsidian | Level 7

Hi Cynthia,

Thing is this:

 

I have a couple of codes like office visit codes, well child visit codes. 

So currently it is two sets of codes:

 

%let Codes1 = "a" "b";   /* office visit codes */

%let Codes2 = "c" "d" "e";   /* well child visit codes */

 

%let allCodes = &Codes1. &Codes2.;   /* this macro var allCodes will contain all codes that were separately defined above */

%put &allCodes.; 

/*  "a" "b" "c" "d" "e" */

 

Eventually, a user will add other codes like dental codes, back pain codes, custom codes like this:

%let Codes3 = "aa" "bb" "cc";  /* dental */

%let Codes4 = "xx" "yy" "zz";  /* Back pain codes */

%let Codes5 = "aaa" "bbb" "ccc"  /* arthritis codes */

 

And I wanted the &allCodes macro variable to now contain all of these additional codes as well without me having to update the code like in this below:

%let  allCodes = &Codes1. &Codes2. &Codes3. &Codes4. &Codes5.;   /* Do not want to append codes like this to update &allCodes because there could be 50 of such code types a user can add*/

 

This is why I was trying to use the macro above..

 

I need to update the &allCodes for future proc freq etc purposes.

 

Cynthia_sas
Diamond | Level 26

When you run this

%let string1 = I like;
%let string2 = Kermit;
%let string3 = the;
%let string4 = Frog;

%put &=string1 &=string2 &=string3 &=string4;


%macro attempt ;
%global allcodes allcodes2;
%let allCodes =;
%do i = 1 %TO 4;

%let allCodes = &allCodes &&string&i;

%if &i = 1 %then %let allcodes2 = (&&string&i;
%else %let allcodes2 = &allcodes2 &&string&i;

%end;
%let allcodes2 = &allcodes2)%str(;);


%put &=allCodes &=allcodes2;

%mend attempt;

%attempt

Then &allcodes should have all 4 strings and allcodes2 should have parens and a semi-colon.

 

Cynthia

rosegarden81
Obsidian | Level 7

Thank you so much for this, I highly appreciate it. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 5 replies
  • 1356 views
  • 2 likes
  • 3 in conversation