BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mmea
Quartz | Level 8

Hi. 

I have a duty to send out data everyday to a reciever.

I want to make this code good for automatisation 

Is there a macro where It reads the file name with different dates every day, without me changing the filename?

 

the filename could be something like this today: test_data_2021-01-19, and tomorrow test_data_2021-01-20

I also have file without the date, so a macro that also can search for the latest file in a directory?

 

 

Hope it is understandable

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

See this quick data step to read all files from a directory and get their last modification timestamp:

%let dir=/folders/myfolders;

data dir;
length
  fref dref $8
  fname $200
;
format dt e8601dt19.;
rc = filename(dref,"&dir.");
did = dopen(dref);
do i = 1 to dnum(did);
  fname = catx("/","&dir.",dread(did,i));
  rc = filename(fref,fname);
  fid = fopen(fref);
  if fid ne 0
  then do;
    dt = input(finfo(fid,foptname(fid,5)),nldatm40.);
    output;
    rc = fclose(fid);
  end;
  else do;
    err = sysmsg();
    put _all_;
  end;
end;
rc = dclose(did);
keep fname dt;
run;

You can then either sort by name or dt to get the latest file, or select for a given name with a given date in it.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

You are not really clear about this.

Do you receive files for importing into SAS, or do you need to export data to external files for sending them to someone else?

mmea
Quartz | Level 8

i receive files from someone into a directory, everyday - and everyday I use these files into my SAS program to come out with a result for them.

 

 

Kurt_Bremser
Super User

See this quick data step to read all files from a directory and get their last modification timestamp:

%let dir=/folders/myfolders;

data dir;
length
  fref dref $8
  fname $200
;
format dt e8601dt19.;
rc = filename(dref,"&dir.");
did = dopen(dref);
do i = 1 to dnum(did);
  fname = catx("/","&dir.",dread(did,i));
  rc = filename(fref,fname);
  fid = fopen(fref);
  if fid ne 0
  then do;
    dt = input(finfo(fid,foptname(fid,5)),nldatm40.);
    output;
    rc = fclose(fid);
  end;
  else do;
    err = sysmsg();
    put _all_;
  end;
end;
rc = dclose(did);
keep fname dt;
run;

You can then either sort by name or dt to get the latest file, or select for a given name with a given date in it.

mmea
Quartz | Level 8

For a file with different dates - where in the code could I state that

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 970 views
  • 4 likes
  • 2 in conversation