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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
ShiroAmada
Lapis Lazuli | Level 10

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.

 

 

Kurt_Bremser
Super User

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;
jurlis
Fluorite | Level 6
Thanks for pointing me to this option "filevar"! This solved my issue.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 5837 views
  • 1 like
  • 3 in conversation