BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 781 views
  • 5 likes
  • 5 in conversation