DATA Step, Macro, Functions and more

How to set a variable filename (INFILE-statement)?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to set a variable filename (INFILE-statement)?

Hi,

 

I'm new to SAS Programming (9.3) and I'm struggling with setting a variable filename for the INFILE-statement.

Goal :

I want to treat a list of SAS logfiles. The full path name of the file is retrieved from a newly created dataset (SASLOGS. LOGFILES). This dataset was created by a 'ls' on a UNIX-directory where SAS logs are stored (see step 1 in code snippet).

Problem:

In the INFILE-statement, I don't succeed in retrieving the correct value of the file -> ERROR: Invalid physical name.

Eventhough, the variable 'saslogfile_fullpath' contains the correct path (with ""). See code snippet.

 

Code snippet :

/*1. Fill SASLOGS.LOGFILES with all filenames listed in directory*/
filename dirlist pipe "ls /sas/prd/common/SASApp_IAM_WorkspaceServer_*.log";
LIBNAME SASLOGS "/sas/prd/data/IAM/SASLOGS";

data SASLOGS.LOGFILES;
infile dirlist lrecl=200 truncover;
input file_name $200.;
run;

 

/* 2. Use SASLOGS.LOGFILES and handle each file (see value "file_name") seperately*/
LIBNAME SASLOGS "/sas/prd/data/IAM/SASLOGS";

%let saslogfile_fullpath=' ';

data work.ONE_LOGFILE;
set SASLOGS.LOGFILES;
saslogfile_fullpath=trim(file_name);
saslogfile_fullpath='"' || strip(saslogfile_fullpath) || '"';
putlog saslogfile_fullpath;/* correct value in saslogfile_fullpath*/

infile &saslogfile_fullpath firstobs=2 length=linelength;

input @1 date YYMMDD10.
+1 time TIME8.
@24 level $5. @;

lengthSASInfo=linelength-52;
input @53 SAS_info $VARYING500. lengthSASInfo;

/* call macro to treat logfile*/
RUN;

 

What I'm I doing wrong?

Thanks for your assistance.

Jurgen


Accepted Solutions
Solution
‎09-13-2017 05:49 AM
Super User
Posts: 7,793

Re: How to set a variable filename (INFILE-statement)?

When you do this:

infile &saslogfile_fullpath firstobs=2 length=linelength;

you reference a macro variable; this reference is resolved once while the datastep is compiled, and requires the macro variable to be present at that time.

A code snippet that reads all lines from a bunch of textfiles into a dataset would look like this:

filename dirlist pipe "ls /sas/prd/common/SASApp_IAM_WorkspaceServer_*.log";

data dirlist;
infile dirlist truncover;
input logfile $200.;
run;

data logtext;
set dirlist;
infilename = logfile;
infile xxx filevar=infilename end=eof truncover;
do until (eof);
  input textline $200.;
  output;
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Frequent Contributor
Posts: 100

Re: How to set a variable filename (INFILE-statement)?

[ Edited ]

Please share your log, thanks.

 

Your first data step based in your code (which i did too) is working fine.  I am not sure about the 2nd data step.  I don't know how different the contents of ls command in unix versus dir command in Windows.

 

 

Solution
‎09-13-2017 05:49 AM
Super User
Posts: 7,793

Re: How to set a variable filename (INFILE-statement)?

When you do this:

infile &saslogfile_fullpath firstobs=2 length=linelength;

you reference a macro variable; this reference is resolved once while the datastep is compiled, and requires the macro variable to be present at that time.

A code snippet that reads all lines from a bunch of textfiles into a dataset would look like this:

filename dirlist pipe "ls /sas/prd/common/SASApp_IAM_WorkspaceServer_*.log";

data dirlist;
infile dirlist truncover;
input logfile $200.;
run;

data logtext;
set dirlist;
infilename = logfile;
infile xxx filevar=infilename end=eof truncover;
do until (eof);
  input textline $200.;
  output;
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 2

Re: How to set a variable filename (INFILE-statement)?

Posted in reply to KurtBremser
Thanks for pointing me to this option "filevar"! This solved my issue.
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 106 views
  • 1 like
  • 3 in conversation