Desktop productivity for business analysts and programmers

Building a filename from prompt variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Building a filename from prompt variable

I am importing text file and want to build the file path based on the month and year from a prompt variable, then use the file path in  an infile statement. Normally strings are my thing but apparently not in SAS. I have included a condensed version of my code to show what I am trying to accomplish. Any advice? 

 

Data myDataSet ;
	%let month = %sysfunc(putn(%sysfunc(month("&PV_ReportMonth."d)), z2.));
	%let year = %sysfunc(year("&PV_ReportMonth."d));

	/* I want to dynamically build the file name and path from a Date Prompt variable &PV_ReportMonth */
	mytextfilepath = "C:\mypath\mytextfile"||@month||@day||".txt";

	/* So if user chose May 2018. mytextfilepath would be "C:\mypath\mytextfile052018.txt"

	/* And use it like this */
	infile mytextfilepath DLM='|' firstobs=2; 
	input Var1 ~$10. 
		Var2 $ 
		Yada $ 
		Blah $;
run;

 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 9,569

Re: Building a filename from prompt variable

Posted in reply to eg_michael

Since the macro statements are resolved before the data step runs, I like to keep them separate in code, for clarity:

%let month = %sysfunc(putn(%sysfunc(month("&PV_ReportMonth."d)), z2.));
%let year = %sysfunc(year("&PV_ReportMonth."d));
%let mytextfilepath = C:\mypath\mytextfile.&month.&day..txt;

Data myDataSet ;
infile "&mytextfilepath" DLM='|' firstobs=2; 
input
  Var1 ~$10. 
  Var2 $ 
  Yada $ 
  Blah $
;
run;

In reality, I would create the macro variable in a preceding data step with call symput, as that removes the need for the %sysfunc calls.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
2 weeks ago
Super User
Posts: 9,569

Re: Building a filename from prompt variable

Posted in reply to eg_michael

Since the macro statements are resolved before the data step runs, I like to keep them separate in code, for clarity:

%let month = %sysfunc(putn(%sysfunc(month("&PV_ReportMonth."d)), z2.));
%let year = %sysfunc(year("&PV_ReportMonth."d));
%let mytextfilepath = C:\mypath\mytextfile.&month.&day..txt;

Data myDataSet ;
infile "&mytextfilepath" DLM='|' firstobs=2; 
input
  Var1 ~$10. 
  Var2 $ 
  Yada $ 
  Blah $
;
run;

In reality, I would create the macro variable in a preceding data step with call symput, as that removes the need for the %sysfunc calls.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 12

Re: Building a filename from prompt variable

Posted in reply to KurtBremser

Perfect, Thank you Kurt!  -  it works perfect Smiley Happy

 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 131 views
  • 0 likes
  • 2 in conversation