DATA Step, Macro, Functions and more

Macro Help

Reply
Occasional Contributor
Posts: 9

Macro Help

[ Edited ]

I am ultimately trying to call the program and have the &strOutYrMo from my macro be assigned to gblYrMo in a new program where I am calling the macro. 

 

When I run my datastep, it is correctly assigning strOutYrMo to equal 201804. But it isn't getting passed along to gblYrMo when I call the program like I think it should. 

 

OPTIONS MPRINT SYMBOLGEN MLOGIC;
%Read_In_Text_File_For_Date(&strTxtPath, gblYrMo);
OPTIONS NOMPRINT NOSYMBOLGEN MLOGIC;
MACRO:

 


%MACRO Read_In_Text_File_For_Date(strInDatePath, strOutYrMo);
	%GLOBAL spMyPath;
	%ResolveFolderLocation(varToStorePath='spMyPath');
	*Write the result to the log;
	%PUT spMyPath=&spMyPath;
	*Create and put strDatePath to log;
	%LET strDatePath=%SYSFUNC(CATS(&spMyPath, Param_Date.txt));
	%PUT &strDatePath;
	%DeleteFileOnSasServer("&strDatePath");
	%LET strInPath = &strInDatePath;
	%PUT &strInPath;

	/*%SYMDEL strInDatePath;*/
	/*Transfer from the network, file two described above which is the list of jobs user wants to run*/
%FtpFromNetworkToLinux(&strInPath
, &spMyPath /*Folder location where you want to send the file*/);
	*Create a dataset that returns the date;
	%PUT &strDatePath;

	DATA AutoSysDate;
		strPath="&strDatePath";

		/*read file in as SAS data set*/
		INFILE DUMMY FILEVAR=strPath;
		INPUT strInDate :$1000.;
		FORMAT dtDate MMDDYY10.;

		IF UPCASE(TRIM(strInDate))='CURRENT' /*Checks the imported file for the word current */
		THEN
			dtDate=mdy(Month(Today()), 1, YEAR(TODAY())) - 1;

		/*If current is found, creates a date for the last day of the previous month*/
		ELSE
			dtDate=mdy(INPUT(SUBSTRN(strInDate, 5, 2), 2.)+1, 1, 
				INPUT(SUBSTRN(strInDate, 1, 4), 4.)) - 1;

		/*If current is not found, assigns the variable the last day of the month provided, YYYYMM format must be provided if Current isn't used */
		strDate=CAT(YEAR(dtDate), PUT(Month(dtDate), z2.));

		/*Creates str variable of YYYYMM*/
		CALL SYMPUTX('strOutYrMo', strDate);

		/*Puts the variable to a macro variable*/
	RUN;

	%PUT &strOutYrMo;
	%DeleteFileOnSasServer("&strDatePath");
	%SYMDEL spMyPath;
%MEND;

*Read_In_Text_File_For_Date;

 

Super User
Posts: 10,280

Re: Macro Help

You are creating macro variable strOutYrMo in the local symbol table, not a macro variable the name of which is in &strOutYrMo.

Do

call symputx("&strOutYrMo",strDate,'g');

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,583

Re: Macro Help

Are you expecting this statement:

%Read_In_Text_File_For_Date(&strTxtPath,gblYrMo);

To place something into gblYrMo? SAS Macro are not functions that return values to parameters in the macro call as some other language procedures/function do in general. The main purpose of SAS macro's is generating SAS code, not data processing.

 

 

To assign a value to a macro variable you are going to have to mack gblRyMo a Global macro variable, if it is not already, and then assign the value with

%let gblYrMo = &strOutYrMo;

 

Or instea of creating a macro variable value that has the same name as a parameter change your Call Symputx to place value into the variable you want.

 

You also include several not defined in the question macros. So we have no idea what they may actually be doing that may impact on this question.

 

By the way you can replace

THEN dtDate = mdy(Month(Today()),1,YEAR(TODAY())) - 1;

with

 

dtdate =intnx('month', today(),-1,'E');

INTNX is the function to adjust dates and times by intervals, in this case a month, the -1 means one month prior and the 'E' indicates returning the end of the month ('B' would be the beginning of the month (or week or quarter or other intervals possible)

 

And if

ELSE dtDate = mdy(INPUT(SUBSTRN(strInDate,5,2),2.)+1,1,INPUT(SUBSTRN(strInDate,1,4),4.)) - 1

is to create a date from a value like yyyymm (201804 for example as 30APR2018 last day of the moth) try

 

dtdate =intnx('month', input(strInDate,yymmN6.),0,'E'); 

In either case instead of

 

strDate = CAT(YEAR(dtDate),PUT(Month(dtDate),z2.)); /*Creates str variable of YYYYMM*/

try

 

strDate= put(dtdate, yymmN6.);

SAS provides a fairly large number of different INFORMATS for reading dates, time and datetime values in common (and some uncommon to many users) layouts. And has even more ways of using formats to display them.

 

Occasional Contributor
Posts: 9

Re: Macro Help

I figured it out. Needed to change the following line:

 

CALL SYMPUTX('strOutYrMo',strDate); /*Puts the variable to a macro variable*/

 

CALL SYMPUTX("&strOutYrMo",strDate); /*Puts the variable to a macro variable*/

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 93 views
  • 0 likes
  • 3 in conversation