*filename pgm '/mscdc/sasmacro/spltsas.sas'; %GLOBAL REVDATE; %LET REVDATE=10APR95; *<----DATE OF LAST REVISION; /***********************************************************************/ /* NAME: spltsas SYSTEM: MSCDC PGMR: Blodgett */ /* FUNCTION: Splits a SAS data set into a series of smaller sets */ /* with names based on the value of a character value stored in */ /* the data set. Can be used, for example, to split a u.s. file */ /* with a state var into 51 sets named per that variable's values. */ /* In most cases you will need to preprocess the set to create the */ /* variable used to name the subsets. */ /* */ /* STATUS: Working under AIX, SAS6.09 */ /* */ /* NOTES: Has option to create index on input set to make it work */ /* more efficiently. Index can be deleted when macro completes. */ /* If index is created and run bombs or you need to rerun you MUST */ /* turn off the genindex option on subsequent runs to avoid error. */ /* */ /* If input set is very large and/or has many different values for */ /* the partitioning variable then this macro will generate 1 DATA ste p*/ /* per value of that variable and will read the entire input set */ /* once for each of those values. Use of an index will reduce the */ /* amount of i/o required but it may still be quite significant. */ /* Designed more for expediency and ease of use rather than efficiency.*/ /***********************************************************************/ /********************************************************************/ /* BEGIN MACRO DEFINITION */ /********************************************************************/ %macro spltsas( setin=, /* input sas data set. user should issue libname if necessary */ pathout=, /* required parm specifying directory for output data lib. CAUTION! Program will write over any files in this dir that have the same name as an existing file. If you want to use the work libref get its pathname from the libname window. */ engine=, /* optional. Engine spec for output set, e.g. xport */ setvar=, /* required. Names a character var in setin that will be used to partition and name the output sets. Values of this variable must be valid sas dataset names, i.e. 1-8 chars, 1st char a letter or '_' */ dropsvar=1,/* the &setvar variable will be dropped from the output data sets unless you specify dropsvar=0 */ genindex=0, /* specify genindex=1 if you want the macro to generate a step to create an index on the setin data set. Note that if an index for that variable already exists specifying this option will cause an error and program will not execute. */ debug=0); %PUT %STR( ); %PUT ***************************************************************; %PUT * SPLTSAS MACRO REV &REVDATE BEGIN EXECUTION *; %PUT * MISSOURI STATE CENSUS DATA CENTER *; %PUT ***************************************************************; %PUT %STR( ); *--This program splits a sas data set into sub data sets based on the value of a character variable within the input set.; %if &genindex %then %do; %let inlib=%scan(&setin,1,'.'); %let inset=%scan(&setin,2,'.'); %if &inset eq %then %do; %let inset=&setin; %let inlib=work; %**put inlib= &inlib inset= &inset; %end; proc datasets ddname=&inlib; modify &inset; index create &setvar; run; %end; proc sort data=&setin(keep=&setvar) out=setnames nodupkeys; by &setvar; run; data _null_; set setnames end=last; _nsets+1; length setparm $8; setparm='set'||left(put(_nsets,4.)); length frstchar $1; frstchar=lowcase(&setvar); if length(&setvar) gt 8 or verify(frstchar, '_abcdefghijklmnopqrstuvwxyz') then do; put // '***invalid value for sas data set: ' &setvar=; end; call symput(setparm,&setvar); setparm='l'||setparm; lsetvar=lowcase(&setvar); call symput(setparm,lsetvar); *<--lower case value of parm used for unix file names--; if last then call symput('nsets',put(_nsets,4.)); run; %do i=1 %to &nsets; %let set=&&set&i; %let lset=&&lset&i; %if &engine=xport %then %let libdsn=&pathout/&lset ; %else %let libdsn=&pathout; %if &i=1 or &engine=xport %then %do; libname out &engine "&libdsn"; %end; %put **Begin copying to &pathout , set name:&lset; data out.&lset; set &setin(where=(&setvar="&set")); %if &dropsvar %then %str( drop &setvar; ); run; %end; %*<--do i loop-; %put ; %put ***** spltsas - generated steps completed ***** ; %MEND spltsas;