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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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