/************************************************************************************************* NAME: GENATTR SYSTEM: MSCDC PGMR: Schimanko, Blodgett FUNCTION: Access SAS dataset & generate file with ATTRIB statements describing all vars on that dataset. Typical use: libname mysas '....'; filename attrib catalog 'sasuser.profile.attrib.source'; %genattr(setin=mysas.myset) Results in a source file being written to your sasuer catalog with a series of Attrib statements specifying the name, length, type, format, informat and label of all variables on mysas.myset. You can then include this code as part of another program that needs to create a similar set. Or you can write an external file and send it to people with your raw data in CSV format so they can use the attrib file to recreate the structure and then import the data into it. STATUS: Modifying to run under Version 8, 6-2001, jgb RELATED MODULES: DOCUMENTATION: in-line comments all there is for now (6/01). NOTES: Basic algorithm from: Karl Schimanko Institut fuer Medizinische Computerwissenschaften *******************************************************************/ %GLOBAL REVDATE; %LET REVDATE=24SEP14; *<----DATE OF LAST REVISION; /*---Update History: 3-14-95: Converting for unix use. 6-19-01: Alter to work with V8 - longer names and labels. Instead of deleting tics in labels replace with double tics. Increase ls value to 150. Alter way in which the code is generated in the post-sql step. 6-20-01 On second thought eliminate sql step and replace with proc contents. 9-24-14 Adding decimal point after format/informat names if none there. E.g. "format title $char" is now "format title $char." --; --- */ /********************************************************************/ /* BEGIN MACRO DEFINITION */ /********************************************************************/ %macro genattr( setin=, /* required. input sas data set. cannot use _last_ */ fileout=attrib, /* fileref of output file containing attrib stmt */ /* it can also be a direct filename enclosed in quotes */ ls=150, /* linesize of output file */ debug=0); %PUT %STR( ); %PUT ***************************************************************; %PUT * GENATTR MACRO REV &REVDATE BEGIN EXECUTION *; %PUT * MISSOURI CENSUS DATA CENTER *; %PUT ***************************************************************; %PUT %STR( ); %local libname memname; %if %index(&setin,.) %then %do; %let memname=%upcase(%scan(&setin,2,.)); %let libname=%upcase(%scan(&setin,1,.)); %end; %else %do; %let memname=%upcase(&setin); %if %sysfunc(libref(user)) %then %let libname=WORK; %else %let libname=USER; %end; %if &debug %then %put libname= &libname %str( ) memname= &memname; %*---old sql step to reference dictionary being replace with proc contents. Much faster when lots of large data libs are present.---; proc contents noprint data=&setin noprint out=_attribs(keep=name label format informat type length varnum); run; proc sort data=_attribs; by varnum; run; %if &debug %then %do; proc print data=_attribs; title3 '_attribs'; run; title3 ' '; %end; Data _null_; File &fileout linesize=&ls; Set _attribs end=eof; If _N_=1 Then Put '*---Attrib statement(s) generated by genattr macro---;' / @2 'Attrib '; else if mod(_N_,20)=1 then Put ';' / @2 'Attrib '; *<--new stmt for each 20; if type=2 then atype='$'; else atype=''; *--handle tics in label values--; length _l $&ls ; *---Add this code to correct bug 9/24/2014 9:00:40 AM---; if format ne ' ' then if index(format,'.')=0 then format=trim(format)||'.'; if informat ne ' ' then if index(informat,'.')=0 then informat=trim(informat)||'.'; _l=trim(name)||' length='||atype||left(put(length,4.)); if format ne ' ' then _l=trim(_l)||' format=' || format ; if informat ne ' ' then _l=trim(_l)||' informat=' || informat; if label=' ' then put _l ; else do; label=tranwrd(label,"'","''"); if length(_l) + length(label) + 10 > &ls then do; put _l / @5 @ ; _l=' '; end; _l=trim(_l)||" label='" ||trim(label)||"'"; put _l; end; If eof Then Put ';'; run; %mend genattr;