BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10


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

3 REPLIES 3
Reeza
Super User
Use SASHELP.VTABLE to find the latest data set. You can use either the created or modified date or you can parse the date from the file name.

smantha
Lapis Lazuli | Level 10
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;

Reeza
Super User

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;


 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1035 views
  • 0 likes
  • 3 in conversation