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

Hello

I want to create a macro variable YYYYMM that is created by concatenation of  MACRO varaibles YYYY and MM.

When MM=09 and YYYY=2021 I expect to get 202109  but I get 20219 in way2 and way3.

Why way2 and way3 are not working as I desired?

What is the solution to adjust way2 and way3 to the desired outcome 202109?

%let MM=09;
%put &MM; /*09*/
%let YYYY=2021;
%put &YYYY; /*2021*/
/**Way1**/
Data _null_;
sas_date=MDY(&MM.,1,&YYYY.);
YYYYMM=put(sas_date,YYMMn6.);
call symput('YYYYMM',YYYYMM);
Run;
%put &YYYYMM.;
/**Way2-no good because get 20219**/
%let YYYYMM= %sysfunc(catS(&YYYY.,&MM.));
%put &YYYYMM.;
/**Way3-no good because get 20219**/
data _null_;
call symput('YYYYMM2',catS(&YYYY.,&MM.));
run;
%put &YYYYMM.;
 

  

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

try this

call symput('YYYYMM',catS("&YYYY.","&MM."));

 

or

 

%let YYYYMM= %sysfunc(catS(&YYYY.&MM.));

View solution in original post

9 REPLIES 9
japelin
Rhodochrosite | Level 12

try this

call symput('YYYYMM',catS("&YYYY.","&MM."));

 

or

 

%let YYYYMM= %sysfunc(catS(&YYYY.&MM.));

Tom
Super User Tom
Super User

The %SYSFUNC(CATS()) is doing nothing in that statement. 

Once the macro variables are resolved you already have the string.

%let YYYYMM= &YYYY.&MM.;

Or in data step

yyyymm_var = "&yyyy.&mm.";
andreas_lds
Jade | Level 19

Working as expectable: The arguments of cats are treated as numerics, so leading zeros are stripped. You should drop %sysfunc from your syntax and just use:

%let yyyymm = &yyyy.&mm.;
Ronein
Onyx | Level 15

What is the reason for difference between these 2 ways?


%let YYYY=2021;
%let MM=09;
/***Way A***/
data _null_;
call symput('YYYYMM',catS("&YYYY.","&MM."));
run;
%put &YYYYMM.;/*Get 202109*/


/***Way B***/
%let YYYYMM= %sysfunc(catS("&YYYY.","&MM."));
%put &YYYYMM.;/*Get "2021""09"*/
andreas_lds
Jade | Level 19

@Ronein wrote:

What is the reason for difference between these 2 ways?


%let YYYY=2021;
%let MM=09;
/***Way A***/
data _null_;
call symput('YYYYMM',catS("&YYYY.","&MM."));
run;
%put &YYYYMM.;/*Get 202109*/


/***Way B***/
%let YYYYMM= %sysfunc(catS("&YYYY.","&MM."));
%put &YYYYMM.;/*Get "2021""09"*/

Because quotes are not necessary in the macro-world of sas, they are not treated as chars identifying a string, but as content of the variable.

Kurt_Bremser
Super User

The macro processor is a text processor and does not need quotes to identify text as such. Quotes are part of text for the macro processor.

 

Write the above down repeatedly until you run out of paper or have finally internalized it.

Kurt_Bremser
Super User

You overcomplicate things by a mile. In macro language, concatenation of texts is done in a simple %LET:

%let yyyymm = &yyyy.&mm.;

A data step is only needed if you need to set macro variables from dataset values, or want to simplify calculations and avoid %SYSFUNC-avalanches.

PaigeMiller
Diamond | Level 26

If it was up to me, I would change the correct answer to the one provided by @andreas_lds , as %SYSFUNC(CATS()) is not needed to concatenate macro variables (which is a good lesson to learn).

--
Paige Miller
PaigeMiller
Diamond | Level 26

I am curious as to why a problem would begin with have macro variables &YYYY and &MM, as opposed to starting with an actual date.

 

If you start with an actual date, you don't have to pull out the year into &YYYY and then pull out the month into &MM and then combine them. If you start with a date, then creating a macro variable &YYYYMM is trivial using formats.

 

It seems as if the problem starts at the wrong point, and if you just go back a little further to start with a date, there is no need to do the work to separate the year from the month and then re-combine them.

--
Paige Miller

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
  • 9 replies
  • 3298 views
  • 9 likes
  • 6 in conversation