DATA Step, Macro, Functions and more

Saving Formatted Text to a Macro Variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 483
Accepted Solution

Saving Formatted Text to a Macro Variable

 

How can I save the marked line as a macro variable containing text like 06?

 

PROC FORMAT;
value Fiscal_Period_Format
1='07'
2='08'
3='09'
4='10'
5='11'
6='12'
7='01'
8='02'
9='03'
10='04'
11='05'
12='06';
RUN;

 

%macro setDates();

      %let currentFiscalPeriod = input(month(date()), Fiscal_Period_Format.); /*this line blows up*/

      %put &currentFiscalPeriod;

      %mend;
%setDates();

 

 


Accepted Solutions
Solution
‎12-16-2015 10:25 AM
Frequent Contributor
Posts: 130

Re: Saving Formatted Text to a Macro Variable

You need some %sysfunc() functions in order to do some of those functions as they are not macro functions.  Try the following:

 

proc format;
	value fiscal_period_format
		1='07'
		2='08'
		3='09'
		4='10'
		5='11'
		6='12'
		7='01'
		8='02'
		9='03'
		10='04'
		11='05'
		12='06';
run;


%macro setDates;
	%let currFiscalPd=%sysfunc(month(%sysfunc(date())),fiscal_period_format.);
	%put &currFiscalPd;
%mend;
%setDates;

View solution in original post


All Replies
Solution
‎12-16-2015 10:25 AM
Frequent Contributor
Posts: 130

Re: Saving Formatted Text to a Macro Variable

You need some %sysfunc() functions in order to do some of those functions as they are not macro functions.  Try the following:

 

proc format;
	value fiscal_period_format
		1='07'
		2='08'
		3='09'
		4='10'
		5='11'
		6='12'
		7='01'
		8='02'
		9='03'
		10='04'
		11='05'
		12='06';
run;


%macro setDates;
	%let currFiscalPd=%sysfunc(month(%sysfunc(date())),fiscal_period_format.);
	%put &currFiscalPd;
%mend;
%setDates;
Super User
Posts: 5,085

Re: Saving Formatted Text to a Macro Variable

This program has several issues (don't we all?), but all can be fixed.

 

  1. Macro language does not execute DATA step functions.
  2. Even if you overcome issue #1, macro language would not execute PUT or INPUT functions.
  3. You are attempting to use INPUT with a format, when PUT would be the right tool.

%SYSFUNC is required to allow macro language to execute a DATA step function.  Unfortunately, that clutters the program significantly.  Try it this way:

 

%let currentFiscalPeriod = %sysfunc(putn(%sysfunc(month(%sysfunc(date()))), Fiscal_Period_Format.));

 

If this gives you any trouble, try removing the dot from the format name:   ... Fiscal_Period_Format));

 

Good luck.

Super User
Super User
Posts: 7,407

Re: Saving Formatted Text to a Macro Variable

My question would be, why do you need to do this in macro language?  There doesn't seem to be any value, and simple Base SAS would do this just fine:

data _null_;
   call symput('currentFiscalPeriod',input(month(date()),fiscal_period_format.);
run;

Saying that, not sure why you need the format either, why does z2. not suffice?

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 279 views
  • 0 likes
  • 4 in conversation