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

Hi friends i am using SAS 9.3, SAS EG 5.1 through citrix and running below code to get all log files but dataset getting created with 2 variables: filename/file_name with same value:

/usr/bin/ksh: dir: not found

I believe this is something happening because of pipe option is not enable for SAS EG: do we have any other way to get all files without using "pipe" option?

Thank you allSmiley Happy

__________________________________________

%let dir=/sas/test/logs;

%let ext=log;

data files;

   infile "dir /b &dir/*.&ext" pipe truncover;

   input filename $300.;

   file_name=scan(filename,1,'.');

run;

_________________________________________

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Pipe wouldn't work if option NOXCMD is set - but then you would get a permission error. The error you get looks more like something with your command isn't correct. Does it work directly in a command prompt like Putty?

You're trying to execute a DOS DIR command in a UNIX/Linux environment. That can't work. You need to use UNIX command LS instead.

View solution in original post

11 REPLIES 11
Patrick
Opal | Level 21

Pipe wouldn't work if option NOXCMD is set - but then you would get a permission error. The error you get looks more like something with your command isn't correct. Does it work directly in a command prompt like Putty?

You're trying to execute a DOS DIR command in a UNIX/Linux environment. That can't work. You need to use UNIX command LS instead.

TomKari
Onyx | Level 15

You can use the "External Files" routines to get information about files within a directory.

Tom

RW9
Diamond | Level 26 RW9
Diamond | Level 26

More interestingly, why are you trying to find a list of log files?  Do you not know what you have run?  Just keep track of what is run in your main programs, or batch run system.

BrunoMueller
SAS Super FREQ

As already mentioned you can use the "External File" functions of the DATA Step to get this information, see also SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition

Below is a sample program that will get a list of files with a given extension.

%macro getFileList(
      dirName=
      , extType=
      , outDsn=
      )
;
 
%let extType = %upcase(&extType);

  data &outdsn;
    length
      msg $
256
      fileRef $
8
      dirName $
1024
      fileName $
256
      fullName $
1281
    ;

   
*
    * check if dirName exist
    *;

    rc_check = fileexist(
"&dirName");

    if rc_check =
0 then do;
      putlog "ERROR: &sysMacroname &dirName does not exist";
      stop;
    end;

   
*
    * assign a fileref to the directoy, let SAS decide on the fileref used
    *;

    rc_assign = fileName(fileRef,
"&dirName");

   
*
    * open the directory
    * if open fails write message and clear fileref
    *;

    dirId = dopen( fileRef );

    if dirId =
0 then do;
      msg = sysmsg();
      putlog
"ERROR: &sysMacroname could not open &dirName as directory";
      putlog msg;
      rc_assign = fileName( fileRef );
      stop;
    end;

   
*
    * open was sucessfull
    * get the number of files in the directory
    *;

    n_files = dnum( dirId );

    do i =
1 to n_files;

     
*
      * get the names of the files and check for the extension
      * we do not check whether the file returned is a file or directory
      *;

      fileName = dread(dirId, i);

      if upcase(scan(fileName, -
1, ".")) = "&extType" then do;
        dirName = "&dirName";
        fullName = catx(
"\", dirName, fileName);
        n_match + 1;
        output;
      end;
    end;

   
*
    * clean up
    *;

    dirId = dclose( dirId );
    rc_assign = fileName( fileRef );
    msg = catx(
" ", "NOTE: &sysMacroname found", n_match, "files that match &extType");
    putlog msg;
    keep
      dirName fileName fullName
    ;
  run;

%mend;

*
* test the macro
*;

%
getFileList(
  dirName=c:\temp
  , extType=csv
  , outDsn=work.csvList
  )
ronan
Lapis Lazuli | Level 10

SAS can perfectly handle by itself the requests to the filesystem, and usually doesn't require external system commands . See above the nice code provided by Bruno. Pipe is for smoking, or Shell scripting in short Smiley Wink but SAS has already plenty of resource instead.

See also this page : http://www.sascommunity.org/wiki/SAS_Filesystem_Toolbox

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

Thank you so much all...!!!

i used unix command (ls-l) in my code and it worked fine.

Appreciate your time....

ruch
Fluorite | Level 6

I am also facing the same issue with below code :-

filename filelist pipe '/data/userdata';


Data F_List;                                       

Infile filelist truncover;

Input filename $100.;

Put filename=;

run;

woo can you please provide your input here so i can resolve my error.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Its a good idea to start a new post with a new question, the only reason I noticed this is because I had posted.  It depends on your operating system, for windows the command is:

filename filelist pipe 'dir c:/data/userdata /b';

So the bit after the quote is sent to the operating system - if you want to see this, goto start menu and choose Command Prompt.  In here if you type dir /? you will see the help for the dos directory command.  If you are on another operating system your commands will be different.

ruch
Fluorite | Level 6

its differnt i am using EG but (compute server ) data is on unix system.Any how i resolves it with DOS command and ls option.

Thanks for prompt response 🙂  

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Again, please open a new post.  If you are using a unix system, then you need to send the unix commands through, not DOS.  If you make a new problem, and ask people to input based on Unix system, someone will be able to help, its been too long since I used Unix to be able to remember the commands.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 11 replies
  • 6044 views
  • 12 likes
  • 8 in conversation