• Home
  • Profile
  • Papers
  • SAS programs

SAS programs
  • Write catalog content to text file
  • Create a document per format encountered
  • Dataset desciption of all dataset in the library
  • Effective questionnaire processing with Arrays
  • Proc transpose
  • Proc SQL where clause
  • Proc SQL dataset maintenance
  • Execute a macro with the value from a dataset observation
  • Change a format stored in the formats catalog
  • Create an empty dataset
Home Write catalog content to text file

Write catalog content to text file

[Download] [Print]
%******************************************************************************;
%** http://www.info-net.nl                                                   **;
%******************************************************************************;
%* Topic         : Utilities                                                  *;
%* Program       : CatalogSourceFiles2TextFiles.sas                           *;
%* Author        : Raymond Ebben                                              *;
%* Location      : http://www.info-net.nl/index/sas-Programs/utilities        *;
%* Date          : February 2007                                              *;
%* Version       : 1.0                                                        *;
%* Description   : This program will search for catalogs within the specified *;
%*                 libraries to extract the source files within these catalogs*;
%*                 to the specified file location. The output will be either  *;
%*                 files (all files in one directory), or directory (each     *;
%*                 library will have a directory and each catalog will be a   *;
%*                 sub directory containing the entries within the catalog.)  *;
%******************************************************************************;
%* In datasets   : None                                                       *;
%* In macrovars  : None                                                       *;
%* In files      : None                                                       *;
%* Out datasets  : None                                                       *;
%* Out macrovars : None                                                       *;
%* Out files     : One file per source entry in the catalog                   *;
%******************************************************************************;
%** Version control                                                          **;
%******************************************************************************;
%* Mod * Ver. * Date      *  Description                                      *;
%******************************************************************************;
%******************************************************************************;
 
%******************************************************************************;
%* Create a macro that will search the libraries specified for catalogs       *;
%* From these catlogs the source entries will be written out to a file.       *;
%* in the specified location                                                  *;
%******************************************************************************;
%macro CatalogSourceFiles2TextFiles(libnames=,location=,outputstyle=directory);
 
  options noxwait;
  %****************************************************************************;
  %* Define the variables to be used within this macro                        *;
  %****************************************************************************;
  %local _currlib;
  %local _member;
  %local _mname;
  %local _lib;
  %local _cat;
  %local _ent;
  %local _library;
  %local _catalog;
  %local _entry;
 
  %****************************************************************************;
  %* Set the name of the macro to be used with the messages                   *;
  %****************************************************************************;
  %let _mname=MACRO: CatalogSourceFiles2TextFiles;
 
  %****************************************************************************;
  %* Get the path to the current SAS folder by defining a library with a dot  *;
  %* then deassign the library as it is not required anymore                  *;
  %****************************************************************************;
  libname _currlib '.';
  %let _currlib=%sysfunc(pathname(_currlib));
  libname _currlib;
 
  %****************************************************************************;
  %* Check if the libname parameter is filled                                 *;
  %****************************************************************************;
  %if (%quote(&libnames) =  ) %then %do;
    %put %str(ERROR:(&_mname) No librarie name was specified         );
    %put %str(ERROR:(&_mname) Macro execution aborted due to error(s));
    %goto endmacro;
  %end;
 
  %****************************************************************************;
  %* Verification of the location parameter.                                  *;
  %* If it is not filled a warning will be issued stating that the current    *;
  %* path will be used. If it is filled, is is ensured that the value ends    *;
  %* with a directory separator. Then it is verified that the location        *;
  %* actually exists. Finally is is checked that the location starts with a   *;
  %* drive specification (second character is a colon)                        *;
  %****************************************************************************;
  %if (%quote(&location) =  ) %then %do;
    %put %str(WARNING:(&_mname) No file location specified                );
    %put %str(WARNING:(&_mname) SAS current path will be used: (&_currlib));
  %end;
  %else %do;
    %if %substr(&location,%length(&location),1) ne \ %then
    %let location=&location\;
 
    %if not %sysfunc(fileexist("&location")) %then %do;
      %put %str(ERROR:(&_mname) Location specified does not exist             );
      %put %str(ERROR:(&_mname) The current value is: &location               );
      %put %str(ERROR:(&_mname) Macro execution aborted due to error(s)       );
      %goto endmacro;
    %end;
    %if %substr(&location,2,1) ne : %then %do;
      %put %str(ERROR:(&_mname) Location does not contain a drive letter      );
      %put %str(ERROR:(&_mname) The current value is: &location               );
      %put %str(ERROR:(&_mname) Macro execution aborted due to error(s)       );
      %goto endmacro;
    %end;
  %end;
 
  %****************************************************************************;
  %* Check if outputstyle contains a valid selection                          *;
  %****************************************************************************;
  %if %quote(%lowcase(&outputstyle)) ne %quote(directory) and
      %quote(%lowcase(&outputstyle)) ne %quote(file) %then %do;
    %put %str(ERROR:(&_mname) Parameter outputstyle contains an invalid value);
    %put %str(ERROR:(&_mname) The current value is: &outputstyle             );
    %put %str(ERROR:(&_mname) valid values: directory, file                  );
    %put %str(ERROR:(&_mname) Macro execution aborted due to error(s)        );
    %goto endmacro;
  %end;
 
  %****************************************************************************;
  %* Create a dataset that contains the names of all available source entries *;
  %* within the libraries specified                                           *;
  %****************************************************************************;
  data _data_;
    set sashelp.vcatalg(keep=libname memname objname objtype
                        where=(upcase(libname) in (%upcase(&libnames))
                               and objtype='SOURCE'));
  run;
 
  %****************************************************************************;
  %* Sort the dataset by libname memname                                      *;
  %****************************************************************************;
  proc sort data=_last_;
    by libname memname;
  run;
 
  %****************************************************************************;
  %* Derive macro variables in an array like structure, representing the      *;
  %* source entries encountered within the libraries specified                *;
  %****************************************************************************;
  data _null_;
    set _last_;
    length libnamecnt memnamecnt objnamecnt 8;
    retain libnamecnt memnamecnt objnamecnt 0;
    by libname memname;
    if first.libname then do;
      libnamecnt=libnamecnt+1;
      memnamecnt=0;
      call symput('_libname'||compress(put(libnamecnt,8.)),compress(libname));
    end;
    if first.memname then do;
      memnamecnt=memnamecnt+1;
      objnamecnt=0;
      call symput('_libname'||compress(put(libnamecnt,8.))||
                  'catalog'||compress(put(memnamecnt,8.)),compress(memname));
    end;
 
    objnamecnt=objnamecnt+1;
    call symput('_libname'||compress(put(libnamecnt,8.))||
                'catalog'||compress(put(memnamecnt,8.))||
                'entry'||compress(put(objnamecnt,8.)),compress(objname));
 
    if last.libname then do;
      call symput('_libname'||compress(put(libnamecnt,8.))||
                  'catalog',compress(put(memnamecnt,8.)));
    end;
    if last.memname then do;
    call symput('_libname'||compress(put(libnamecnt,8.))||
                'catalog'||compress(put(memnamecnt,8.))||
                'entry',compress(put(objnamecnt,8.)));
    end;
 
    call symput('_libname',compress(put(libnamecnt,8.)));
  run;
 
  %****************************************************************************;
  %* Remove the dataset used to create the macrovariables                     *;
  %****************************************************************************;
  proc sql noprint;
    drop table &syslast;
  quit;
 
  %****************************************************************************;
  %* If no entries are found send a message to the log and skip the outputting*;
  %****************************************************************************;
  %if not %symexist(_libname) %then %do;
    %put %str(NOTE:(&_mname) No source entries encountered.);
    %put %str(NOTE:(&_mname) libraries specified: &libnames);
  %end;
  %else %do;
 
    %**************************************************************************;
    %* Loop through the source entries encountered and export them to an      *;
    %* external file in the specified location                                *;
    %**************************************************************************;
 
    %**************************************************************************;
    %* Set the location to the specified drive                                *;
    %**************************************************************************;
    x "%substr(&location,1,2)";
    %if %length(&location) > 3 %then %do;
      x "cd %substr(&location,3)";
    %end;
 
    %do _lib=1 %to &_libname;
 
      %let _library=&&_libname&_lib;
      %if not %sysfunc(fileexist("&location&_library")) %then %do;
        x "md &_library";
      %end;
      x "cd &_library";
 
      %do _cat=1 %to &&_libname&_lib.catalog;
 
        %let _catalog=&&&&_libname&&_lib.catalog&_cat;
        %if not %sysfunc(fileexist("&location&_library&_catalog")) %then %do;
          x "md &_catalog";
        %end;
        x "cd &_catalog";
 
        %do _ent=1 %to &&&&_libname&&_lib.catalog&_cat.entry;
 
          %let _entry=&&&&_libname&&_lib.catalog&_cat.entry&_ent;
 
            data _null_;
              infile "&_library..&_catalog..&_entry..source" catalog;
              %if %quote(%lowcase(&outputstyle)) eq %quote(file) %then %do;
                file "&location&_library._&_catalog._&_entry..txt";
              %end;
              %else %do;
                file "&location&_library\&_catalog\&_entry..txt";
              %end;
 
              input @1;
              put _infile_;
            run;
 
        %end;
        x "cd..";
 
      %end;
 
    %end;
 
  %end;
 
%endmacro:
 
%mend CatalogSourceFiles2TextFiles;
 
%CatalogSourceFiles2TextFiles(libnames=%str("sashelp"),location=%str(c:\temp\));
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
 

© 2010 Raymond Ebben, expert SAS consultant and programmer, Netherlands All Rights Reserved.