Desktop productivity for business analysts and programmers

without using "pipe" option

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 318
Accepted Solution

without using "pipe" option

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;

_________________________________________


Accepted Solutions
Solution
‎04-08-2015 07:08 PM
Respected Advisor
Posts: 3,837

Re: without using "pipe" option

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


All Replies
Solution
‎04-08-2015 07:08 PM
Respected Advisor
Posts: 3,837

Re: without using "pipe" option

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.

Trusted Advisor
Posts: 1,060

Re: without using "pipe" option

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

Tom

Esteemed Advisor
Posts: 6,698

Re: without using "pipe" option

Second what Patrick said. Computers != Windows, your server runs on UNIX, use the appropriate commands.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,249

Re: without using "pipe" option

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.

SAS Super FREQ
Posts: 676

Re: without using "pipe" option

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
  )
Frequent Contributor
Posts: 117

Re: without using "pipe" option

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

Super Contributor
Super Contributor
Posts: 318

Re: without using "pipe" option

Thank you so much all...!!!

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

Appreciate your time....

Occasional Contributor
Posts: 7

Re: without using "pipe" option

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.

Esteemed Advisor
Esteemed Advisor
Posts: 7,249

Re: without using "pipe" option

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.

Occasional Contributor
Posts: 7

Re: without using "pipe" option

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 :-)  

Esteemed Advisor
Esteemed Advisor
Posts: 7,249

Re: without using "pipe" option

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.

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 1667 views
  • 10 likes
  • 8 in conversation