Hi SAS users,
I need help with reading a FILENAME from macro parameter & import it. I am able to read the file if i hardcode it in the program, but macro parameter is not working.
%MACRO TESTING(FILENAME=,KEY=,TABLE=);
proc import datafile= '/testpath/&FILENAME' /* this is currently resoving as '/testpath/&FILENAME.xlsx not found */
out=TEST_DATASET
DBMS=XLSX
replace;
run;
DataTest_DS;
set TEST_DATASET;
run;
%MEND;
%TESTING (FILENAME ='TEST_FILE',KEY='GREEN',TABLE=GREEN_TABLE);
I see two issues:
1. In the macro defintion, you should use double quotes around the file path, i.e. "/testpath/&FILENAME". Macro variables do not resolved inside of single quotes. So currently it is looking for an Excel file that named &FILENAME.xlsx.
2. When you call the macro, you should not put quotes around the values you pass to the FileName parameter. Try:
%TESTING (FILENAME =TEST_FILE,KEY='GREEN',TABLE=GREEN_TABLE);
If you put the quote marks, they become part of the value. And your PROC EXPORT statement would have extra quote marks, so would look like: proc import datafile= "/testpath/'TEST_FILE'"
One way to check these things is to turn on options MPRINT, which will show in the log the SAS code that is generated by the macro. It's very helpful for debuggin macros.
I see two issues:
1. In the macro defintion, you should use double quotes around the file path, i.e. "/testpath/&FILENAME". Macro variables do not resolved inside of single quotes. So currently it is looking for an Excel file that named &FILENAME.xlsx.
2. When you call the macro, you should not put quotes around the values you pass to the FileName parameter. Try:
%TESTING (FILENAME =TEST_FILE,KEY='GREEN',TABLE=GREEN_TABLE);
If you put the quote marks, they become part of the value. And your PROC EXPORT statement would have extra quote marks, so would look like: proc import datafile= "/testpath/'TEST_FILE'"
One way to check these things is to turn on options MPRINT, which will show in the log the SAS code that is generated by the macro. It's very helpful for debuggin macros.
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.
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.