BookmarkSubscribeRSS Feed
pchappus
Obsidian | Level 7

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;

 

3 REPLIES 3
Kurt_Bremser
Super User

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');

 

ballardw
Super User

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.

 

pchappus
Obsidian | Level 7

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*/

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 3 replies
  • 721 views
  • 0 likes
  • 3 in conversation