BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eg_michael
Fluorite | Level 6

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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.

eg_michael
Fluorite | Level 6

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

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 1192 views
  • 0 likes
  • 2 in conversation