SAS Macros To Create .CSV Output Using Proc Export

This program exports the ID and site information for Capture-Recapture Analysis of subjects in a longitudinal data study using R. The dynamic .CSV export macro loops through the numeric values of a given variable in a dataset using that value to both subset the data and modify the output file’s name.

Note: This program was developed on a 32-bit Windows system.

libname ks "S:nfoxKansasDatasetsPERK";
libname kansas "S:nfoxKansasDatasets"; *formats are saved here;
options fmtsearch=(kansas);
* ------------------------------------------------------------------------------- ;
* Create Macros for CSV file export ;
* ------------------------------------------------------------------------------- ;
%macro exportCSV(dset=,where=,outfile=,drop=gender VAR2);
DATA FOO(drop=&drop.); 
     SET &dset. (where=(&where.));
RUN;
PROC EXPORT DATA=FOO 
            OUTFILE=&outfile. 
            DBMS=CSV REPLACE;
     PUTNAMES=YES;
RUN;
%mend exportCSV;

%macro exportDynamicCSV;
%do exportLoop = 1 %to 4;
%exportCSV(where=ageGroup2=&exportLoop.,
           outfile="S:nfoxKansasProgramscapturecaptureKS-ageGroup&exportLoop..csv");
%end;
%mend exportDynamicCSV;
* ------------------------------------------------------------------------------- ;
* Recode site-related data for Capture Analysis ;
* ------------------------------------------------------------------------------- ;
data ks.ids; 
     retain idnum stf_id1 site1 stf_id2 recap1 stf_id3 recap2 stf_id4 recap3
                  stf_id5 recap4 stf_id6 recap5;
     length site1 recap1-recap5 3;
     set ks.stf (keep=idnum stf_id1-stf_id6 stf_14 stf_15 stf_4 fd_7 ageGroup gender);

     year=scan(stf_15,-1);

     if fd_7=1 and (stf_14=0 or (stf_14=1 and year >= 2008)) and stf_4
         in('66757','67301','67333','67335','67337','67340','67344','67347',
        '67351','67363','67364','66753','67330','67332','67335','67336',
        '67337','67341','67342','67351','67354','67356','67357');

     site1=scan(stf_ID1,1,'-');
     array recap {5} recap1-recap5;
     array sites {5} stf_ID2-stf_ID6;
     do i = 1 to 5;
	    if sites{i} ne '-999' then do;
	       recap{i}=scan(sites{i},1,'-');
	       if site1=recap{i} then recap{i} = -999;
	    end;
	    if sites{i} eq '-999' then do;
	       recap{i}=sites{i};
	    end;
   	    if recap2 ne -999 then do;
	       if recap2 = site1 then recap2=-999;
               if recap2 = recap1 then recap2=-999;
	    end;
 	    if recap3 ne -999 then do;
	       if recap3 = site1 then recap3=-999;
               if recap3 = recap1 then recap3=-999;
	       if recap3 = recap2 then recap3=-999;
	    end;
	    if recap4 ne -999 then do;
	       if recap4 = site1 then recap4=-999;
               if recap4 = recap1 then recap4=-999;
	       if recap4 = recap2 then recap4=-999;
	       if recap4 = recap3 then recap4=-999;
   	    end;
            if recap5 ne -999 then do;
	       if recap5 = site1 then recap5=-999;
               if recap5 = recap1 then recap5=-999;
	       if recap5 = recap2 then recap5=-999;
	       if recap5 = recap3 then recap5=-999;
	       if recap5 = recap4 then recap5=-999;
	    end;
     end;
	 IF AGEGROUP IN(1,2) THEN AGEGROUP2=1;
	 IF AGEGROUP IN(3,4) THEN AGEGROUP2=2;
	 IF AGEGROUP IN(5,6) THEN AGEGROUP2=3;
	 IF AGEGROUP >=7  THEN AGEGROUP2=4;
drop i stf_id1-stf_id6 stf_14 stf_15 stf_4 fd_7 year ageGroup; 
run;
* ------------------------------------------------------------------------------- ;
* Call Macros and export CSV files ;
* ------------------------------------------------------------------------------- ;
%exportCSV(where=1,outfile="S:nfoxKansasProgramscapturecaptureKS.csv")
%exportCSV(where=gender=1,outfile="S:nfoxKansasProgramscapturecaptureKS-malePop.csv")
%exportCSV(where=gender=2,outfile="S:nfoxKansasProgramscapturecaptureKS-fmPop.csv")
%exportDynamicCSV

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>