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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 187 views
  • 1 like
  • 3 in conversation