DATA Step, Macro, Functions and more

Extracting source elements from catalog - how to?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Extracting source elements from catalog - how to?

[ Edited ]

I have "inherited" as SAS catalog that contains several dozen program elements. I need to pull the elements out into individual SAS program files. Seems like a simple task... I just can't seem to figure out how to do it. :-)  

 

Using desktop SAS on Windows 7, if that matters. 

 

Edit: 

What I'm hoping to be able to do is get the names of the program elements in the catalog (I know how to do this) and write a SAS program that will take each program element and output it as a SAS program file.  

 

So something like    MyLib.Mycat.MyProgram1.Source      becomes     d:\MyFolder\Myprogram1.sas

 

I'd rather not have to manually save and name each file for referential integrity with other programs that access the catalogs.

 


Accepted Solutions
Solution
‎11-07-2016 04:56 PM
Super User
Super User
Posts: 7,076

Re: Extracting source elements from catalog - how to?

Posted in reply to JSWilson64

Just use a DATA _NULL_ step to copy the lines.

%let memname=fred ;
data _null_;
  infile "work.mycat.&memname..source" catalog ;
  file "~/test/&memname..sas";
  input;
  put _infile_;
run;

View solution in original post


All Replies
Super User
Posts: 3,260

Re: Extracting source elements from catalog - how to?

Posted in reply to JSWilson64

The FILENAME statement can help you here:

 

http://support.sas.com/documentation/cdl/en/lestmtsref/68024/HTML/default/viewer.htm#n0e720f80cp61wn...

 

You can then use %include or from your command line INCLUDE fileref to bring the source code into the SAS editor.

Occasional Contributor
Posts: 18

Re: Extracting source elements from catalog - how to?

I don't know if %include will work, since that submits the code and all I want to do is save it as a ProgramName1.sas file. 

 

I edited my original post with more info on what I'm trying to do. Thanks!

Super User
Posts: 3,260

Re: Extracting source elements from catalog - how to?

[ Edited ]
Posted in reply to JSWilson64

Just use INCLUDE fileref then - that will extract the source program into the SAS editor without running it. From there you can SAVE it.

 

filename dir catalog 'mylib.mycatalog';
dm 'include dir(prog1)';
Super User
Posts: 19,869

Re: Extracting source elements from catalog - how to?

Posted in reply to JSWilson64

%COPY?

 

http://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/viewer.htm#p0oltlodtnemugn10...

 

This assumes that the source was actually saved in the catalog, and not just the compiled program....if it's just the compiled program you might be out of luck. 

Solution
‎11-07-2016 04:56 PM
Super User
Super User
Posts: 7,076

Re: Extracting source elements from catalog - how to?

Posted in reply to JSWilson64

Just use a DATA _NULL_ step to copy the lines.

%let memname=fred ;
data _null_;
  infile "work.mycat.&memname..source" catalog ;
  file "~/test/&memname..sas";
  input;
  put _infile_;
run;
Occasional Contributor
Posts: 18

Re: Extracting source elements from catalog - how to?

Thanks, Tom. This worked great, with the least amount of effort on my part. I used the CONTENTS option in Proc Catalog to list out the objects in the catalog, then used Excel to build some repetitive data step statements to save each item out into a SAS program file with the same name.

Super User
Super User
Posts: 7,076

Re: Extracting source elements from catalog - how to?

[ Edited ]
Posted in reply to JSWilson64

Excel????????  Why the heck would you use Excel to generate SAS code?

 

%let catalog=work.mycat ;
%let dir=~/test ;
filename code temp;
data _null_;
  set sashelp.vcatalg ;
  where libname="%upcase(%scan(&catalog,1,'.'))"
    and memname="%upcase(%scan(&catalog,2,'.'))"
    and objtype='SOURCE'
  ;
  length catalog fname $200 ;
  catalog=catx('.',"&catalog",objname,objtype);
  fname = catx('/',"&dir",cats(lowcase(objname),'.sas'));
  file code ;
  put 'data _null_;'
    / '  infile ' catalog :$quote. 'catalog;'
    / '  file ' fname :$quote. ';'
    / '  input;'
    / '  put _infile_;'
    / 'run;'
  ;
run;
%include code ;

   

Super User
Posts: 10,044

Re: Extracting source elements from catalog - how to?

Posted in reply to JSWilson64

data pgm=stored.sample;
describe;
run;

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 389 views
  • 0 likes
  • 5 in conversation