DATA Step, Macro, Functions and more

SAS Import File

Reply
SAS Employee
Posts: 73

SAS Import File

I have following SAS code that looks for "DailySales" files and import daily sales data into SAS data set. What i would like to do is the code to search for the "DailySales" files and if it doesn't find the "DailySales" files then use the "DailyImport" file.

[pre]
Data work.DailySales ;
INFILE "C:\DailySales"
LRECL=143
ENCODING="WLATIN1"
TERMSTR=CRLF
TRUNCOVER ;
.....
[/pre]
Valued Guide
Posts: 2,177

Re: SAS Import File

the infile option FILEVAR= allows you to define the physical file name at runtime ( so, after you check that the file exists).
The FILEEXIST() function allows your data step to decide which to read
Super User
Posts: 10,041

Re: SAS Import File

Hi.Another choice is to use filename + pipe.

[pre]
filename file pipe 'dir C:\DailySales /B';

data temp;
infile file truncover;
input fname $20.;
fname=coalesce(fname,'DailyImport');
......
[/pre]




Ksharp
SAS Employee
Posts: 73

Re: SAS Import File

Thanks for all reply. I will try both methods.
Super User
Posts: 10,041

Re: SAS Import File

Hi.
Sorry.My code is wrong.
It is my fault that not test it. Please ignore me.


Ksharp
Super User
Posts: 10,041

Re: SAS Import File

OK.I will give you a solution.The following code is right. I test it by myself.
[pre]
%let path=c:\;
filename fname pipe "dir &path.dailysales.txt &path.dailyimport.txt /B";
data want;
infile fname truncover;
input fname $20.;
fname="&path"||fname;
infile dummy filevar=fname end=last length=len;
do until( last);
input row $varying200. len;
output;
if last then stop;
end;
run;
[/pre]



Ksharp
SAS Employee
Posts: 73

Re: SAS Import File

Ksharp,

I'm using unix and getting error for using pipe:

ERROR: Insufficient authorization to access PIPE.

%let path="/mnt/data/";
filename fname pipe "/mnt/data/ &path.dailysales.txt &path.dailyimport.txt /B";
SAS Employee RMP
SAS Employee
Posts: 52

Re: SAS Import File

There could be a number of reasons. From where are you running this code? Base SAS, EGuide?

If you are running it from Eguide, it may be that the object spawner is not allowed to execute external OS commands. You will need to add a NONOXCMD to the object spawner startup command.

Just looked again at your code. What exactly are you trying to do in the PIPE statement? You are missing the DIR in the filename statement.

Message was edited by: RMP Message was edited by: RMP
Super Contributor
Super Contributor
Posts: 3,174

Re: SAS Import File

Suggest the OP search the SAS support website for additional info.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

ERROR: Insufficient authorization to access PIPE site:sas.com
Super User
Posts: 10,041

Re: SAS Import File

Under Unix, you should use ' ls ' command to check the list of directory.
But from your log, it is to say you do not have right to use PIPE functionality.
If you can ,contact with SAS Administrator, to see whether to open it for you.
If you can not ,can refer to the SAS code which used fopen() function written by SASKiWi at previous post.

Ksharp
Super User
Posts: 10,041

Re: SAS Import File

Or you can code like this.But need to change the path into unix (/mnt/data/trans.txt)


[pre]
filename a 'c:\trans.txt';

%let a=%sysfunc(fexist(a) );

%macro imp;
%if &a=1 %then %do;
proc import datafile='c:\trans.txt' out=want dbms=tab replace;
getnames=yes;
run;
%end;
%else %do;
proc import datafile='c:\dailysales.txt' out=want dbms=tab replace;
getnames=yes;
run;
%end;
%mend imp;

%imp
[/pre]


Ksharp
Ask a Question
Discussion stats
  • 10 replies
  • 305 views
  • 0 likes
  • 5 in conversation