BookmarkSubscribeRSS Feed
awmeyertimmy
Fluorite | Level 6

Hello everyone,

 

I'm using Base SAS 9.4.  I'm trying to import an Excel file into a SAS data set (which I know how to do).  The problem is, there are hundreds of files in a folder and I need to import the file with the most recent Date Modified.  If it helps at all, every file follows the same naming convention.  Example:  FileABCD_123456.  The file with the maximum value of the six digits following the underscore is the file I want to import.  Can anyone help?  Much appreciated!!

 

-Adam

9 REPLIES 9
ed_sas_member
Meteorite | Level 14

Hi @awmeyertimmy 

 

You can try the following code to retrieve the number of the file in the macrovariable &num_file

eg. &num_file will take the value 123456 in your example, if 123456 is the maximum value among several numbers.

 

NB: this code assumes the number does not exceed 8 digits.

 

proc sql noprint;
	select max(input(prxchange('s/^.+_//',1,trim(memname)),8.))
	into: num_file trimmed
	from dictionary.tables
	where libname = "WORK" and prxmatch('/_\d+$/',trim(memname));
quit;

proc sql;
	select memname
	from dictionary.tables
	where libname = "WORK" and scan(trim(memname),2,"_")="&num_file";
quit;

NB: please adapt your libname in the WHERE clause.

 

awmeyertimmy
Fluorite | Level 6

@ed_sas_member  does that "dictionary.files" work if the files are csv files or do they need to be sas data sets.  The reason I ask is that I am getting no observations (however the code executes successfully)

ed_sas_member
Meteorite | Level 14

Hi @awmeyertimmy 

I am sorry, because I just realize that I made a mistake when I read your question: I thought you talked about SAS datasets and not other kind of file like csv.

To answer your question, the dictionary.tables table stores a lot of information regarding SAS datasets and is accessible via proc sql.

ed_sas_member
Meteorite | Level 14

Hi @awmeyertimmy 

please have a look at @Tom ‘s answer (like below) to retrieve file names in a SAS dataset.

 

/* untested code */

 

data files (keep=Memname);
length memname $256;
	fich=filename('fich',"&path");
	did=dopen('fich');
	nb_fich=dnum(did);
	do i=1 TO nb_fich;
		memname=dread(did,i);
		output;
	end;
	rc=dclose(did);
run;
Tom
Super User Tom
Super User

Get the list of files. Parse out that numeric suffix. Find the maximum suffix.  Put the name into a macro variable. Use the macro variable in place of the hard coded name in the code to read one Excel file.

 

The first part is easier if you know what type of operating system SAS is running on and you are allowed to run operating system commands.  Otherwise you will need to resort to use the DREAD() function. (check the on-documentation for an example).

 

Assuming you are running on Windows you would just use the DIR command to get the list of files.

%let path=\\servername\sharename\foldername;
data files:
  infile "dir &path\*.xlsx /b" pipe truncover ;
  input filename $256. ;
  length suffix $32 number 8;
  suffix = scan(filename,-2,'._');
  number = input(suffix,??32.);
run;

proc sql noprint;
  select filename into :fname trimmed
  from files where number = max(number)
  ;
quit;

Now just use "&path/&fname" as the name of the xlsx file to convert.

awmeyertimmy
Fluorite | Level 6

I'm sorry, yes I am running Windows.  When I run that code it says that I have insufficient authorization to access PIPE.  So unfortunately, that isn't working for me.  Thank you though!

SASKiwi
PROC Star

Talk to your SAS administrator about enabling use of OS commands from SAS. By default this is switched off in server installations.

Tom
Super User Tom
Super User

Then you will have resort to a slightly more complex solution to get the list of files.

Make a fileref pointing to the directory. Open the directory with DOPEN(), And then read the names using DREAD().  You will need to add a test for whether the file has XLSX extension on its name or not.

%let path=\\servername\sharename\foldername;
 
data files;
  length filename $256 suffix $32 number 8;
  keep filename suffix number;
  length rc did index 8 fileref $8 ;
  rc=filename(fileref,"&path");
  did=dopen(fileref);
  do index=1 to dnum(did);
    filename=dread(did,index);
    if upcase(scan(filename,-1,'.')) = 'XLSX' then do;
      suffix = scan(filename,-2,'._');
      number = input(suffix,??32.);
      output;
    end;
  end;
run;

 

 

awmeyertimmy
Fluorite | Level 6

@ed_sas_member @Tom @SASKiwi Thanks everyone for your help! Much appreciated!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 9 replies
  • 2134 views
  • 2 likes
  • 4 in conversation