LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (February 2009, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Wed, 11 Feb 2009 15:57:14 +0000
Reply-To:   karma <dorjetarap@GOOGLEMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   karma <dorjetarap@GOOGLEMAIL.COM>
Subject:   Re: using macro as function
Comments:   To: Gradowsky <marchi.gianluca@libero.it>
In-Reply-To:   <908fcdc2-39d3-41f1-a4ce-78b6876ac110@a12g2000yqm.googlegroups.com>
Content-Type:   text/plain; charset=ISO-8859-1

The problem with your code here is the semicolon in your macro. When this resolves in your datastep, the parser encounters the semicolon before the closing brackets are seen, raising an error to the log.

However, using string functions on a date when there are perfectly good functions for dates, doesn't seem like a good idea.

If you want to use the SAS date functions, just make sure you use the correct date informats first. Then if you are interested in the absolute difference between two dates, you can use the abs function.

HTH

%MACRO FUNC(MONTH); SUBSTR(PUT(&MONTH,Z6.),1,4)*12 + SUBSTR(PUT(&MONTH,Z6.),5,2)*1 %MEND; %macro elapsed_months(start, end); abs(floor((intck('month',&start,&end)))) %mend;

DATA TEST; format m1 m2 month1 month2 date9.; M1=200901; M2=200705; DIFF=(%FUNC(M1))- (%FUNC(M2)); month1 = input(put(m1,best6. -L),yymmn6.); month2 = input(put(m2,best6. -L),yymmn6.); DIFF2=%elapsed_months(month1,month2); RUN;

2009/2/11 Gradowsky <marchi.gianluca@libero.it>: > Hi All, > > I have problem with the following code. I want to calculate the > difference (in months) between two months specified as yyyymm. for > example I have M1=200901 and M2=200705, than the difference is 20 > months. > > %MACRO FUNC(MONTH); > SUBSTR(PUT(&MONTH,Z6.),1,4)*12 + SUBSTR(PUT(&MONTH,Z6.),5,2)*1; > %MEND; > > DATA TEST; > M1=200901; M2=200705; > DIFF=%FUNC(M1)-%FUNC(M2); > RUN; > > and this is the log message I get: > > NOTE: Line generated by the invoked macro "FUNC". > 1 ((SUBSTR(PUT(&MONTH,Z6.),1,4)*12) + (SUBSTR(PUT(&MONTH,Z6.),5,2) > *1)); > > - > > 79 > ERROR 79-322: Expecting a ). > > MPRINT(FUNC): ((SUBSTR(PUT(m1,Z6.),1,4)*12) + (SUBSTR(PUT(m1,Z6.),5,2) > *1)); > 108 diff=(%func(m1)) - (%func(m2)); > - > 180 > ERROR 180-322: Statement is not valid or it is used out of proper > order. > > SYMBOLGEN: Macro variable MONTH resolves to m2 > SYMBOLGEN: Macro variable MONTH resolves to m2 > MPRINT(FUNC): ((SUBSTR(PUT(m2,Z6.),1,4)*12) + (SUBSTR(PUT(m2,Z6.),5,2) > *1)); > 108 diff=(%func(m1)) - (%func(m2)); > - > 180 > ERROR 180-322: Statement is not valid or it is used out of proper > order. > > > Well, first of all I am wondering why it is asking for an additional > parenthesis, and in general, why it doesn't work. > > have you any idea to help me? >


Back to: Top of message | Previous page | Main SAS-L page