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.;
try this
call symput('YYYYMM',catS("&YYYY.","&MM."));
or
%let YYYYMM= %sysfunc(catS(&YYYY.&MM.));
try this
call symput('YYYYMM',catS("&YYYY.","&MM."));
or
%let YYYYMM= %sysfunc(catS(&YYYY.&MM.));
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.";
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.;
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"*/
@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.
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.
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.
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).
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.