Help using Base SAS procedures

Creating Macros Based on months

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 87
Accepted Solution

Creating Macros Based on months

Hi,

I am trying to create macros based on month

eg., if month is JAN FEB MAR then i need to  create year macro -1 year

else i want to create macro +1 year

if Month JAN/FEB/MAR 2012 then i want to create macro year 2011

else i want to create macro year 2012


Accepted Solutions
Solution
‎03-26-2012 06:10 AM
Super User
Posts: 10,023

Creating Macros Based on months

%let day='02jul2012'd ;

options minoperator;
%macro  a;
%if %sysfunc(substr(%sysfunc(putn(&day,monyy.)),1,3)) in JAN FEB MAR %then 
 %do;data _null_;call symputx('year',year(&day)-1);stop;run;%end;
 %else %do;data _null_;call symputx('year',year(&day));stop;run;%end;
%mend a;
%a
%put &year ;


Ksharp

View solution in original post


All Replies
Frequent Contributor
Posts: 139

Creating Macros Based on months

Hi,

i the macro value (+1 year)  (-1year) like that?

Thanks & Regards,

Manoj

Frequent Contributor
Posts: 87

Creating Macros Based on months

Posted in reply to manojinpec

yes

Creating macro year based on months conditionally

if month is JAN/FEB/MAR 2012 then want to create macro year=2011

else macro year=2012

Respected Advisor
Posts: 3,156

Creating Macros Based on months

And if the 'date'  goes with your system time dynamically,  you don't need to feed it:

data _null_;

if month(date()) in (1,2,3) then

call symputx('year',year(date())-1);

else call symputx('year',year(date()));

run;

%put &year;

Regards,

Haikuo

Frequent Contributor
Posts: 87

Creating Macros Based on months

Thanks for support

can we select the date insted of system date the report can be generated for any month the date should vary

march to march

Solution
‎03-26-2012 06:10 AM
Super User
Posts: 10,023

Creating Macros Based on months

%let day='02jul2012'd ;

options minoperator;
%macro  a;
%if %sysfunc(substr(%sysfunc(putn(&day,monyy.)),1,3)) in JAN FEB MAR %then 
 %do;data _null_;call symputx('year',year(&day)-1);stop;run;%end;
 %else %do;data _null_;call symputx('year',year(&day));stop;run;%end;
%mend a;
%a
%put &year ;


Ksharp

Frequent Contributor
Posts: 87

Creating Macros Based on months

Thanks Ksharp

it is working fine

title of program is report generated for the period 31 Dec 2011 for financial year 2011-2012

(if month is from april 2011 to march 2012)

if month is April 2012 then title will be " for financial year 2012-2013"

i am unable to put other date

Respected Advisor
Posts: 3,156

Creating Macros Based on months

Data _null_'s code will for you then.

Respected Advisor
Posts: 3,799

Re: Creating Macros Based on months

Shifted interval...

29   %let day='02jul2012'd ;

30   %let year=%sysfunc(intnx(Year1.4,&day,0),year.);

31   %put YEAR=&YEAR;

YEAR=2012

35   %let day='02feb2012'd ;

36   %let year=%sysfunc(intnx(Year1.4,&day,0),year.);

37   %put YEAR=&YEAR;

YEAR=2011

Frequent Contributor
Posts: 87

Creating Macros Based on months

Posted in reply to data_null__

Thanks for  your support

i want to generate a title

if the report generating period is form April 2011 to Mar2012 then title=

"for the financial year 2011 to 2012"

if the report is generating for period dec 2011 then title will be "for the financial year 2011-2012"

if the report is generating for period jan 2012 then title will be " for the financial year 2011-2012"

Respected Advisor
Posts: 3,156

Creating Macros Based on months

I maybe misunderstand your question, but could it be done by just  using the title statement?

Supposely you have already run _null_'s code:

Title "for the financial year &year.- %eval(&year.+1)";

Haikuo

Respected Advisor
Posts: 3,799

Re: Creating Macros Based on months

How about this.

38   %let day='02feb2012'd ;

39   %let year=%sysfunc(intnx(Year1.4,&day,0,B),year.)-%sysfunc(intnx(Year1.4,&day,0,E),year.);;

40   %put YEAR=&YEAR;

YEAR=2011-2012

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 479 views
  • 0 likes
  • 5 in conversation