DATA Step, Macro, Functions and more

LIBREF with SAS generated name.

Reply
Respected Advisor
Posts: 3,799

LIBREF with SAS generated name.

I want to create a libref with a SAS generated named.  I can create a fileref with a SAS Generated name using a variable in the name field that is missing.  The function populates the name variable with a generated as #LN00013 see below.  It doesn't work that way for LIBREF in this program.  I know the libref is created I can see it with LIBNAME _ALL_ LIST; it is named WC000001.
18         data _null_;
19            length dd $8;
20            dd = ' ';;
21            rc = libname(dd,'~');
22            msg = sysmsg();
23            put _all_;
24            dd = ' ';
25            rc = filename(dd,'~');
26            msg = sysmsg();
27            put _all_;
28         run;

dd=  rc=
0 msg=  _ERROR_=0 _N_=1
dd=#LN00013 rc=
0 msg=  _ERROR_=0 _N_=1
Respected Advisor
Posts: 4,919

Re: LIBREF with SAS generated name.

Posted in reply to data_null__

I get something different when I run your code on 9.3 Win 7 , 32 bits :

dd=  rc=-70008 msg=NOTE: Library WC000001 does not exist. _ERROR_=0 _N_=1

dd=#LN00010 rc=0 msg=  _ERROR_=0 _N_=1

PG

PG
Respected Advisor
Posts: 3,799

Re: LIBREF with SAS generated name.

Did you use tilde ~? You need the proper path syntax for your OS.  I am UNIX and ~ means "home".

Respected Advisor
Posts: 4,919

Re: LIBREF with SAS generated name.

Posted in reply to data_null__

Yeah. If I use a period (current dir in Windows), I match your output :

dd=  rc=0 msg=  _ERROR_=0 _N_=1

dd=#LN00010 rc=0 msg=  _ERROR_=0 _N_=1

Looks like a case for Tech Support... But then, is name generation an official feature of SAS?

PG

PG
Respected Advisor
Posts: 3,799

Re: LIBREF with SAS generated name.

It seem like is must be a feature because SAS does generate the name and define the libref, note RC=0.  I just don't know how to get SAS to tell me what that name is.

But you may be right "we ain't s'pose to it that way".

Super User
Posts: 11,343

Re: LIBREF with SAS generated name.

Posted in reply to data_null__

The automatic macro variable &SYSLAST will have the library and dataset name of the last created dataset. Parse the string for the library

 

%let lastlib = %scan(&syslast,1,'.');

%put &lastlib;

Super User
Super User
Posts: 7,039

Re: LIBREF with SAS generated name.

The code is just making librefs, not creating any datasets, so SYSLAST macro variable will not be updated.

Super User
Super User
Posts: 7,039

Re: LIBREF with SAS generated name.

Posted in reply to data_null__

Seems to be how it works.  Perhaps you can put it on the SAS ballot.

Meanwhile use FILENAME to generate the new name. You will need to modify it slightly to make it valid.

  data _null_;

     length fileref libref $8;

* Make FILEREF ;

     fileref = ' ';;

     rc = filename(fileref,'~');

     msg = sysmsg();

     put _all_;


* Generate LIBREF from FILEREF ;

     libref='_'||substr(fileref,2);


* clear FILENAME ;

     rc = filename(fileref,' ');

     msg = sysmsg();

     put _all_;

* Generate LIBNAME ;

     rc = libname(libref,'~');

     msg = sysmsg();

     put _all_;

  run;

fileref=#LN00023 libref=  rc=0 msg=  _ERROR_=0 _N_=1

fileref=  libref=_LN00023 rc=0 msg=  _ERROR_=0 _N_=1

fileref=  libref=_LN00023 rc=0 msg=  _ERROR_=0 _N_=1

Respected Advisor
Posts: 3,799

Re: LIBREF with SAS generated name.

I thought about that but ended up just using &SYSINDEX.

Ask a Question
Discussion stats
  • 8 replies
  • 475 views
  • 0 likes
  • 4 in conversation