DATA _NULL_;
call symput( "Today", COMPRESS(left( put( date(),YYMMDDn8.) ) )) ;/*today for report date exten*/
Run;
%PUT &Today;
Run;
proc sql;
create table new1 as
select make,model,invoice
from sashelp.cars
where invoice <=20000
;quit;
/*bring in car_id from libname*/
proc sql;
create table cars2 as
select a.make,a.model,a.invoice,b.car_id
from new1 a
inner join gs1.inventory_&Today.
;quit;
The issue is that I need to fetch and use a file called inventory from the gs library. Even though I am showing a macro (&today) to capture the date, the actual date in the libname is not consistent each month. (ie last month the file name was inventory_20200805, two months ago inventory_20200702)
Is there a way to develop or imporve the existing date macro to capture the exact file regardless of the date extension each month. This will enable me to automate the run of the report each month
proc sql;
create table inven as
select memname, input(scan(memname,-1,'_'),yymmdd8n.) as date from sashelp.vtable where
strip(upcase(libname)) = 'GS1'
and index(strip(upcase(memname)),'INVENTORY_')>0
order by date desc;
quit;
data _null_;
set inven;
if _n_ =1 then call symputx('dsname',memname);
run;
* Your code here on using the dataset dsname;
SQL will process character data within summary functions such as max/min. Because the date is in the format YYMMDD it will sort correctly so you can just pick the maximum data set name.
proc sql noprint;
select memname into : mydataset
from sashelp.vtable
where libname = upcase('WORK') and upcase(memname) like 'CLASS%'
having max(memname) = memname;
quit;
%put &mydataset;
Full code to test:
data class_20200813;
set sashelp.class;
run;
data class_20200812;
set sashelp.class;
run;
data class_20200811;
set sashelp.class;
run;
data class_20200810;
set sashelp.class;
run;
data class_20200809;
set sashelp.class;
run;
data class_20200808;
set sashelp.class;
run;
proc sql;
select memname into : mydataset
from sashelp.vtable
where libname = upcase('WORK') and upcase(memname) like 'CLASS%'
having max(memname) = memname;
quit;
%put &mydataset;
@smantha wrote:
proc sql; create table inven as select memname, input(scan(memname,-1,'_'),yymmdd8n.) as date from sashelp.vtable where strip(upcase(libname)) = 'GS1' and index(strip(upcase(memname)),'INVENTORY_')>0 order by date desc; quit; data _null_; set inven; if _n_ =1 then call symputx('dsname',memname); run; * Your code here on using the dataset dsname;
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.