BookmarkSubscribeRSS Feed
bradleyx
Calcite | Level 5

Good Day, I'm trying to automate importing an excel file from a folder based on the most recent time period.

For example,

 

proc import out= cars
datafile= "C:/mydata/SampleData_202304.xlsx"

dbms= xlsx replace;
sheet="Sheet1";
run;

It will import the file for April 2023 but for May 2023 I would have to manually change the file name. How can I automate that?

 

2 REPLIES 2
whymath
Lapis Lazuli | Level 10

You can use the system date instead of writing it manual. Try this:

proc import out= cars
datafile= "C:/mydata/SampleData_%sysfunc(date(),yymmn6.).xlsx"
dbms= xlsx replace;
sheet="Sheet1";
run;
Kurt_Bremser
Super User

If you want the latest from a series of files:

data files;
rc = filename(dref,"C:/mydata");
did = dopen(dref);
if did
then do;
  do i = 1 to dnum(did);
    name = dread(did,i);
    if substr(name,1,11) = "SampleData_" then output;
  end;
  rc = dclose(did);
end;
rc = filename(dref);
keep name;
run;

proc sort data=files;
by descending name;
run;

data _null_;
set files;
call symputx('name',name);
stop;
run;

proc import out= cars
datafile= "C:/mydata/&name."

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 2 replies
  • 721 views
  • 1 like
  • 3 in conversation