UNIX pipes in SAS

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

UNIX pipes in SAS

Hi SAS Users,

 

I was trying to excecute this example given in SAS website to use inmy project & getting warning as libname IN1 to INn does not exist , but it is trying to read all 14 Datasets from the unixpath.

 

filename lsinfo pipe 'ls -d /unixpath/*.sas7bdat';
data _null_;
infile lsinfo pad end=last;
input dsn $50.;
call symput('in'||left(_n_),trim(dsn));
if last then call symput('ndsn',left(_n_));
run;

proc print;
%macro mlibname;
%do i=1 %to &ndsn;
libname in&i "&&in&i";
%end;
%mend;
%mlibname run;

 

 

NOTE: Library IN1 does not exist.
NOTE: Library IN2 does not exist.
NOTE: Library IN3 does not exist.

 

Thanks for helping.


Accepted Solutions
Solution
‎11-21-2016 02:05 PM
Respected Advisor
Posts: 3,837

Re: UNIX pipes in SAS

@KurtBremser

Yes, you are right. I thought the -d switch in the ls command will only return directories - but I was mistaken.

 

@SASAna

Libnames are used to point to folders containing SAS files and not for pointing to individual SAS files. So you simply need to define a single libname.

libname mydata '/unixpath';

 

You can then address SAS files (tables) in this folder via "mydata.<name of sas file>";

 

If you want to list all SAS files (tables) in a folder then you can use syntax like:

libname mydata '/unixpath';
proc sql;
  select name
  from dictionary.tables
  where libname='MYDATA' and memtype='DATA'
 ;
quit;

 

View solution in original post


All Replies
Grand Advisor
Posts: 17,389

Re: UNIX pipes in SAS

What are you trying to do? You should assign a library and then use SAS functions to work with SAS datasets. You can use the pipe to get the information but it's not necessary. 

 

You can use PROC datasets or query the SASHELP.VTABLE dataset to get all dataset names in a library. 

 

If you want to print each dataset I would suggest CALL EXECUTE to generate the proc print code. 

 

 

Esteemed Advisor
Posts: 6,685

Re: UNIX pipes in SAS

.sas7bdat files are SAS datasets.

SAS libraries, OTOH, are directories.

So assigning a libref to a .sas7bdat will never work.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,837

Re: UNIX pipes in SAS

What you're trying to do should work in principal.

 

What I would suggest for debugging, is that you turn on "options symbolgen;" and replace the "data _null_;" with "data test;" 

This should show you where it goes wrong (eg. if your ls command really returns the information in the form you expect it).

 

I'd also reformulate your infile statement to "infile lsinfo truncover end=last;" - or then add an explicit lrecl=... if you want to use pad.

 

 

Esteemed Advisor
Posts: 6,685

Re: UNIX pipes in SAS

@Patrick: The OP is reading a directory listing of *.sas7bdat files into macro variables, and then tries to assign libnames to those files. Won't work.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎11-21-2016 02:05 PM
Respected Advisor
Posts: 3,837

Re: UNIX pipes in SAS

@KurtBremser

Yes, you are right. I thought the -d switch in the ls command will only return directories - but I was mistaken.

 

@SASAna

Libnames are used to point to folders containing SAS files and not for pointing to individual SAS files. So you simply need to define a single libname.

libname mydata '/unixpath';

 

You can then address SAS files (tables) in this folder via "mydata.<name of sas file>";

 

If you want to list all SAS files (tables) in a folder then you can use syntax like:

libname mydata '/unixpath';
proc sql;
  select name
  from dictionary.tables
  where libname='MYDATA' and memtype='DATA'
 ;
quit;

 

Frequent Contributor
Posts: 81

Re: UNIX pipes in SAS

Thanks Much Patrick, this is an easy way to list the Datasets.
☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 278 views
  • 0 likes
  • 4 in conversation