DATA Step, Macro, Functions and more

SAS in Unix: X Command

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

SAS in Unix: X Command

I was able to succesfully count the number of files in the New Folder in unix shell and output it to My SAS files folder

 

cd H:\My_SAS_Files\New_Folder

count='ls | wc -w'

{ echo -n 'Number of Files is ' & eval "$count"; } >> H:\My_SAS_Files\output

 

To Check Number of Files:

cd ..

tail output

 

I need to execute this in SAS to encoperate a loop and some macros, but I'm not sure how to get the X comamnd to work. Any ideas?

 

X "cd H:\My_SAS_Files\New_Folder";
X "count='ls | wc -w'";
X "{ echo -n 'Number of Files is ' & eval "$count"; } >> H:\My_SAS_Files\output";


Accepted Solutions
Solution
‎08-23-2016 02:06 PM
Super User
Super User
Posts: 7,042

Re: SAS in Unix: X Command

Unix is not going to be able to find any files on a directory name that starts with H:.

 

I find is is easier to code using a DATA _NULL_ and the PIPE engine.

 

%let path=/mydirectory ;

data file_count;
  infile "cd &path ; ls -A | wc -l" pipe;
  path="&path";
  input files;
run;

View solution in original post


All Replies
Solution
‎08-23-2016 02:06 PM
Super User
Super User
Posts: 7,042

Re: SAS in Unix: X Command

Unix is not going to be able to find any files on a directory name that starts with H:.

 

I find is is easier to code using a DATA _NULL_ and the PIPE engine.

 

%let path=/mydirectory ;

data file_count;
  infile "cd &path ; ls -A | wc -l" pipe;
  path="&path";
  input files;
run;
Super User
Posts: 10,023

Re: SAS in Unix: X Command

Make a Shell file  xx.sh and put all the command into it ,execute it at SAS side:

X '/home/xx.sh' ;

Super User
Posts: 5,427

Re: SAS in Unix: X Command

Another option would be to use SAS external file functions.

May require some more programming lines the the shell counterpart, but you'll keep all logic in SAS.

 

Yet another option is to first call your shell script, then call SAS with your SAS program, avoiding the OS call from within SAS.

Data never sleeps
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 1027 views
  • 3 likes
  • 4 in conversation