Hi All,
I would like to use a SAS variable in my proc SQL's from statement, in order to create a dynamic query to automate monthly data requests.
I would like to retrieve a table from a server, from a specific folder. Lets call the folder "MyFolder".
There are monthly data table uploads to this folder, using a time stamp like yyyymmdd, where dd is the last day of the previous month.
I would like my code, to always get the right date, without any extra manual work, and retrieve the appropriate table.
And since %SYSFUNC allows formatting of the result, your function call would look like this:
%let TableDate = %sysfunc(intnx(month,%sysfunc(today()),-1,e),yymmn4.);
%put &=tabledate;
We're missing some information, like what is the value of the macro variable &TodaysDate? And do you have a LIBNAME statement for MYFOLDER? Maybe you could provide more of the code before PROC SQL.
Nevertheless, treating dates as strings is simply a difficult way to proceed. Better to treat them as dates. Something like this:
%let todaysdate = %sysfunc(today());
%let tabledate = %sysfunc(intnx(month,&todaysdate,-1,e));
PROC SQL;
CREATE TABLE WORK.RandomName AS SELECT DISTINCT ID
FROM MyFolder.Table%sysfunc(putn(&tabledate,yymmddn8.))_I
WHERE ID = '12345678' AND CategoryID NOT IN ('45678','123789');
QUIT;
Please note: in the FROM statement, there are no spaces (your original code had spaces in there which won't work)
Please note: you said "yyyymmdd, where dd is the last day of the previous month." If this is something you have designed, make your life easier by using just yyyymm instead of yyyymmdd.
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.
Ready to level-up your skills? Choose your own adventure.