DATA Step, Macro, Functions and more

how to import excel file when its having date at the end

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 111
Accepted Solution

how to import excel file when its having date at the end

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.



Accepted Solutions
Solution
‎05-13-2015 09:58 AM
Super User
Super User
Posts: 7,061

Re: how to import excel file when its having date at the end

Posted in reply to Ravikumarkummari

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


All Replies
Super User
Posts: 7,831

Re: how to import excel file when its having date at the end

Posted in reply to Ravikumarkummari

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,061

Re: how to import excel file when its having date at the end

Posted in reply to Ravikumarkummari

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;

Frequent Contributor
Posts: 111

Re: how to import excel file when its having date at the end

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.

Super User
Super User
Posts: 7,061

Re: how to import excel file when its having date at the end

Posted in reply to Ravikumarkummari

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.

Frequent Contributor
Posts: 111

Re: how to import excel file when its having date at the end

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.

Solution
‎05-13-2015 09:58 AM
Super User
Super User
Posts: 7,061

Re: how to import excel file when its having date at the end

Posted in reply to Ravikumarkummari

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" ....

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 684 views
  • 0 likes
  • 3 in conversation