Hello
I run this code but I see that there is wrong calculation of variable "a".
May anyone explain why did it happen and what is the way to solve it?
%let YYMM1=2302;
%let YYMM2=2306;
data t1;
%let start = %sysfunc(inputn(&YYMM1.,yymmn4.));
%let end = %sysfunc(inputn(&YYMM2.,yymmn4.));
call symput("n",trim(left(intck('month',&start.,&end.))));
a=&n.;
b=MOD(&n.,2);
c=&YYMM1.;
d=&YYMM2.;
run;
/****Varaible a should get value 4!!!!***/
%let YYMM1=2303; %let YYMM2=2306;
data t2;
%let start = %sysfunc(inputn(&YYMM1.,yymmn4.));
%let end = %sysfunc(inputn(&YYMM2.,yymmn4.));
call symput("n",trim(left(intck('month',&start.,&end.))));
a=&n.;
b=MOD(&n.,2);
c=&YYMM1.;
d=&YYMM2.;
run;
/****Varaible a should get value 3 but I see value 4!!!!***/
Did you read the documentation about the "method" option and how it calculates?
[Edit:] Correct link:
Bart
Macro timing.
Macro timing.
Macro timing.
data t1;
%let start = %sysfunc(inputn(&YYMM1.,yymmn4.));
%let end = %sysfunc(inputn(&YYMM2.,yymmn4.));
call symput("n",trim(left(intck('month',&start.,&end.))));
a=&n.; /* this is resolved at compile time, so the value from CALL SYMPUT will not have an effect */
b=MOD(&n.,2); /* same */
c=&YYMM1.;
d=&YYMM2.;
run;
Kurt's right, with macro timing fixed it displays:
a=symgetn('n');
b=MOD(a,2);
c=symgetn('YYMM1');
d=symgetn('YYMM2');
Bart
Instead of this:
call symput("n",trim(left(intck('month',&start.,&end.))));
a=&n.;
(Why do you feel you need to create a macro variable &n here within a data step anyway, when a data step variable will do?)
use this:
a=intck('month',&start.,&end.);
And I would never use macro variables to keep values across actions in a data step. Within a single observation it is simple insanity, and across observations one uses RETAINed data step variables.
Perhaps this is intentionally an exercise on macro evaluation, but, if not, you might consider using the DATA step, which is faster, easier to debug, and more versatile. Perhaps something like this?
%let YYMM1=2302;
%let YYMM2=2306;
data t1;
format start end DATE9.;
start = inputn("&YYMM1.",'yymmn4.');
end = inputn("&YYMM2.",'yymmn4.');
n = intck('month',start,end);
a=n;
b=MOD(n,2);
c=&YYMM1.;
d=&YYMM2.;
run;
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.