BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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!!!!***/

 

 

 

6 REPLIES 6
yabwon
Onyx | Level 15

Did you read the documentation about the "method" option and how it calculates? 

[Edit:] Correct link:

https://documentation.sas.com/doc/en/vdmmlcdc/8.1/lefunctionsref/p1md4mx2crzfaqn14va8kt7qvfhr.htm#n1...

 

https://documentation.sas.com/doc/en/vdmmlcdc/8.1/ds2ref/n096g3nwx05g7wn161ax2rdqmlfn.htm#n121oubah6...

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

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;
yabwon
Onyx | Level 15

Kurt's right, with macro timing fixed it displays:

yabwon_0-1693301778382.png

 

	 a=symgetn('n');
	 b=MOD(a,2);
	 c=symgetn('YYMM1');
	 d=symgetn('YYMM2');

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

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.);

 

--
Paige Miller
Kurt_Bremser
Super User

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.

Rick_SAS
SAS Super FREQ

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;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 6 replies
  • 1326 views
  • 5 likes
  • 5 in conversation