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

Good day all,

I am facing the problem with importing excel file through macros.

the excel name is    DATAExport 2015-05-04-12-18-52

the file is coming every week monday. so i created macro for this.

%LET TODAY1=%SYSFUNC(TODAY(),DATE9.);

%LET LAST_WEEK=%SYSFUNC(INTNX(WEEK,"&TODAY1."D,-1,B),DATE9.);

%LET LAST_WEEK_MONDAY=%SYSFUNC(INTNX(DAYS,"&LAST_WEEK."D,1),YYMMDDD10.);

here I didn't  create macro for time. The file contains time at the end.

Up to  DATAExport 2015-05-04 this i can automate using macro.

proc import datafile="D:\emp\DATAExport &LAST_WEEK_MONDAY..xls"

but what my requirement is what ever the time it is consider only Date. and ignore time.

How to import this excel file. the time is changes from file to file. So i can't importing it successfully.


Any help would be greatly appreciated.


1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Well that's too bad.  The LIBNAME statement is not as smart as the INFILE statement.

%LET TODAY1=%SYSFUNC(TODAY(),DATE9.);

%LET LAST_WEEK_MONDAY=%SYSFUNC(INTNX(WEEK.2,"&TODAY1."D,-1,B),YYMMDDD10.);

%put &=TODAY1 &=&LAST_WEEK_MONDAY;

data _null_;

  length filename $256;

  infile "D:\emp\DATAExport &LAST_WEEK_MONDAY*.xls" filename=filename obs=1;

  input @;

  call symputx('FILENAME',filename);

run;

proc import datafile="&filename" ....

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Since you can build the name up to the date, make a directory listing with the wildcard "*" and then use the name you find; this of course assumes that there'e only one file per day.

This would probably easiest with

filename oscmd pipe "ls filename&date.*.xls";

data _null_;

infile oscmd;

input;

* you may do some checks here to avoid problems if no file is found;

call symput('xls_filename',trim(_infile_));

run;

If you can't do that (NOXCMD), then this could be helpful: http://www.wuss.org/proceedings12/55.pdf

Tom
Super User Tom
Super User

When I tried using a * as wildcard in the DATAFILE option for a delimited file it worked.

%let path=%sysfunc(pathname(work)) ;

proc export data=sashelp.class file="&path\class.txt" dbms=dlm replace ;

run;

proc import out=class datafile="&path\clas*.txt" dbms=dlm replace ;

run;

Ravikumarkummari
Quartz | Level 8

I am not able to understand you both said.

using filename and data _null_ how the import it is done. is it dataset after importing saved to any library.

Tom
Super User Tom
Super User

If you know that there is only one file with that day of week and some unknown time of day in the filename then use a WILDCARD in the filename and SAS and the operating system will find the file without you needing to know the name.


proc import datafile="D:\emp\DATAExport &LAST_WEEK_MONDAY*.xls"

  data=last_week_monday replace

...

If it is possible that there is more than one such file then this method might not work. You will need to test what it does if there are more than one.

Ravikumarkummari
Quartz | Level 8

PROC IMPORT OUT= data_1

            DATAFILE="D:\emp\DATAExport &LAST_WEEK_MONDAY*.xls"

            DBMS=EXCEL REPLACE;

     GETNAMES=YES;

RUN;

ERROR: Connect: Failure creating file.

ERROR: Error in the LIBNAME statement.

Connection Failed.  See log for details.

Tom
Super User Tom
Super User

Well that's too bad.  The LIBNAME statement is not as smart as the INFILE statement.

%LET TODAY1=%SYSFUNC(TODAY(),DATE9.);

%LET LAST_WEEK_MONDAY=%SYSFUNC(INTNX(WEEK.2,"&TODAY1."D,-1,B),YYMMDDD10.);

%put &=TODAY1 &=&LAST_WEEK_MONDAY;

data _null_;

  length filename $256;

  infile "D:\emp\DATAExport &LAST_WEEK_MONDAY*.xls" filename=filename obs=1;

  input @;

  call symputx('FILENAME',filename);

run;

proc import datafile="&filename" ....

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 2784 views
  • 0 likes
  • 3 in conversation