*options obs=200; options compress = yes; libname out '/tmp'; /* Years to read */ /* this program is an example of how to read in multiple years of data */ %let fyr = 90; %let lyr = 99; /* Macro to read data */ %macro cps(first,last); %do yr = &first %to &last; %let rec = 855; %if &yr. <= 97 %then %let rec=814; %if &yr. <= 94 %then %let rec=600; %let mns = 12; %do mn = 1 %to &mns.; %if &mn. < 10 %then %do; filename raw pipe "zcat /home/data/cps-basic/cpsb&yr.0&mn..Z "; %end; %else %do; filename raw pipe "zcat /home/data/cps-basic/cpsb&yr.&mn..Z "; %end; data temp(drop = rectyp); infile raw lrecl = 855 missover length=l ; length age sex month year rectyp prpertyp grade 3; /* Input data for 1989-1993 */ if &yr. in (89,90,91,92,93) then do; input rectyp 144 age 270-271 @; /*--------------------------------------------------------------*/ /* From January 1989 to December 1993: 1 = interviewed adult (record length=594) 2 = type a noninterview (record length=156) 3 = type b/c noninterview (record length=156) 4 = armed forces record (record length=471) 5 = children's record (record length=471) From Sep 1989 to Dec 1991, record length = 600 for all rectyp. From Jan 1992 to Dec 1993: 1 = interviewed adult (record length=600) 2 = type a noninterview (record length=157 or 583) 3 = type b/c noninterview (record length=157 or 583) 4 = armed forces record (record length=471) 5 = children's record (record length=471) A rectyp = 2 or rectyp = 3 record had a lenght of 583 if it had a value for longwgt, the longitudnal weight. Otherwise the length was 157. */ /*--------------------------------------------------------------*/ /* rectyp is RECTYP and a value of 1 indicates an interviewed rectyp */ /* age is A-AGE */ if (rectyp = 1 or rectyp = 5) and age >= 14 and age <= 17 then do; input month 38-39 year 40 sex 275 wgt 398-405 ernwgt 406-413 famwgt 444-451 ; end; *group = 1; if wgt <> . then wgt = wgt/100; else wgt = .; if ernwgt <> . then ernwgt = ernwgt/100; else ernwgt = .; if year <> . and &yr. in (90,91,92,93) then year = year + 1990; else if year <> . and &yr. in (89) then year = year + 1980; else year = .; end; /* Input data for 1994 through 1997 */ if &yr. in (94,95,96,97) then do; input age 122-123 prpertyp 161-162 @; /* age is PRTAGE */ /* prpertyp is PRPERTYP -- a value of 2 indicates an Adult civilian household member; a child is 1 */ /* A correct record has a length of 814 or 815, depending on the length of the variable PXSCHLVL is columns 813-814. A carriage return character follows. */ if (prpertyp = 2 or prpertyp = 1) and age >= 14 and age <= 17 then do; input month 65-66 year 67-68 sex 129-130 grade 137-138 famwgt 583-592 ernwgt 603-612 wgt 613-622 ; end; if wgt <> . then wgt = wgt/10000; else wgt = .; if ernwgt <> . then ernwgt = ernwgt/10000; else ernwgt = .; if ((&yr. = 94) or (&yr. = 95 and &mn. in (1,2,3,4,5,6,7,8))) then group = 3; else group = 4; if year <> . then year = year + 1900; else year = .; end; /* Input data for 1998 and 1999 */ if &yr. in (98,99) then do; input age 122-123 prpertyp 161-162 @; /* age is PRTAGE */ /* prpertyp is PRPERTYP -- a value of 2 indicates an Adult civilian household member; a child is 1 */ /* record length = 855 for all records */ if (prpertyp = 1 or prpertyp = 2) and age >= 14 and age <= 17 then do; input month 16-17 year 18-21 sex 129-130 grade 137-138 famwgt 583-592 ernwgt 603-612 wgt 613-622 wgtbls 846-855 ; end; group = 4; if wgt <> . then wgt = wgt/10000; else wgt = .; if ernwgt <> . then ernwgt = ernwgt/10000; else ernwgt = .; *wgtbls=wgt/10000; end; data cps&yr._&mn.; set temp; proc datasets nolist; delete temp; %end; /* end over months */ /* Merge monthly files into annual dataset */ data cps&yr.; set %month(1,&mns.); run; proc datasets; delete %month(1,&mns.); proc means data=cps&yr.; %end; /* end over years */ %mend cps; /* Save all months together as a permanent dataset */ %macro month (first,last); %do n=&first. %to &last.; cps&yr._&n. %end; %mend month; /* Save all years together as a permanent dataset */ %macro year(first,last); %do yr=&first. %to &last.; cps&yr. %end; %mend year; /* Run the macro for all years */ %cps(&fyr.,&lyr.); /* Set output file name here */ /* for 90-94, will give cps90_94.ssd01 */ /* for 96-99, will give cps96_99.ssd01 */ data out.cps&fyr._&lyr.; set %year(&fyr.,&lyr.); run; proc datasets; delete %year(&fyr.,&lyr); /* Sample stats */ proc means data=out.cps&fyr._&lyr.; *proc sort; *by year; *proc means data=out.cps&fyr._&lyr.; *by year; proc tabulate data=out.cps&fyr._&lyr.; class month year; var wgt wgtbls ; table year,month*wgt*sum*f=comma13.0 ; table year,month*wgtbls*sum*f=comma14.0; table year,month*f=comma9.0;