-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACS_tract_household.sas
891 lines (583 loc) · 15.4 KB
/
ACS_tract_household.sas
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/***************************************************
This script create variables for the sloan project
household at state level
By Jun Zhu
*****************************************************/
libname IPUMS "K:\Housefin3\Monthly\ACS";
libname sloan "J:\housefin2\sloan";
libname cross "K:\Housefin3\Monthly\cross-walks";
/*****************************************************
PUMA 2000 is valid from 2005 through 2011 ACS Data
PUMA 2010 is valid for 2012, 2013, 2014 and 2015 ACS Data
*****************************************************/
/*****change the geo level here *****/
%let geo_level = year state county tract;
%let geolist ="year", "state", "county","tract";
%let lastgeo= tract;
%let datas=household;
%let dset1=puma2000tocensus2010;
%let dset2=puma2010tocensus2010;
/***************************************/
%macro ss_cross_a;
data aa;
set cross.&dset1.;
afact1 = afact*1;
state1=state*1;
puma1=puma2K*1;
run;
%do year=2005 %to 2011;
proc sql;
create table h&datas._&year. as
select
a.*
,b.county as county
,b.tract as tract
,b.afact1 as afact1
from &datas.a_&year. a
inner join aa b
on (a.State =b.state1 and a.puma = b.puma1)
;
quit;
data &datas._&year.;
set h&datas._&year.;
afact= afact1*hhwt;
run;
%end;
%mend ;
/***********************/
%macro ss_cross_b;
data aa;
set cross.&dset2.;
afact1 = afact*1;
state1=state*1;
puma1=puma12*1;
run;
%do year=2012 %to 2015;
proc sql;
create table h&datas._&year. as
select
a.*
,b.county as county
,b.tract as tract
,b.afact1 as afact1
from &datas.A_&year. a
inner join aa b
on (a.State =b.state1 and a.puma = b.puma1)
;
quit;
data &datas._&year.;
set h&datas._&year.;
afact= afact1*hhwt;
run;
%end;
%mend ;
/***cat. variables ***/
/***variables ends with 1 or 2 are created by urban
vacancy rate provided
****/
/*******************************************/
%LET CAT_HOUSEHOLD=SEX_HH AGE1_HH RACE1_HH CITIZEN_HH EDUC_HH EMPSTAT_HH
COMMUSE OWNERSHP MULTGEN NCHILD NCHLT5 NFAMS1 NCOUPLES1 NSUBFAM1;
%LET NUME_HOUSEHOLD=AGE_HH HHINCOME FAMSIZE ;
;
%LET CROSS_CAT_HOUSEHOLD = SEX_HH AGE2_HH RACE1_HH CITIZEN_HH EDUC1_HH EMPSTAT_HH
COMMUSE OWNERSHP MULTGEN
NFAMS1 NCHILD1 NCOUPLES1 NSUBFAM1;
%LET CROSS_NUM_HOUSEHOLD=AGE_HH HHINCOME FAMSIZE ;
%LET CAT_CROSS_HOUSEHOLD1= SEX_HH AGE2_HH RACE1_HH CITIZEN_HH EDUC1_HH EMPSTAT_HH
COMMUSE OWNERSHP MULTGEN
NFAMS1 NCOUPLES1 ;
/**********************************
categories for numeric variables
**********************************/
proc format;
value age_f
low -4 ="Low_4"
5-9 = "5_24"
10-14 = "10_14"
15-19 = "15_19"
20-24 = "20_24"
25-29 = "25_29"
30-34 = "30_34"
35-39 = "35_39"
40-44 = "40_44"
45-49 = "45_49"
50-54 = "50_54"
55-59 = "55_59"
60-64 = "60_64"
65-69 = "65_69"
70-74 = "70_74"
75-79 = "75_79"
80-84 = "80_84"
85-high ="85_High" ;
value age_ff
low -19 = "Low_19"
20-39 = "20_39"
40-59 = "40_59"
60-high ="60_High" ;
value family_f
low -1 ="Low_1"
2 = "2"
3 = "3"
4-high ="4_High" ;
value subfamily_f
low -1 ="Low_1"
2 -high ="2_High" ;
value couple_f
low -0 ="Low_0"
1 ="1"
2= "2"
3 -high ="3_High" ;
value builtyr_f
low -1 ="Low_39"
2 ="40_49"
3= "50_59"
4 = "60_69"
5 ="70_79"
6= "80_89"
7,8 = "90_99"
9 ="00_04"
10,11,12,13,14= "05_09"
15-high = "10_High"
;
value proptx_f
Low-11 = "Low-499"
12-21 = "500-999"
22-31 = "1000-1999"
32-41 = "2000-2999"
42-51 = "3000-3999"
52-62 = "4000-4999"
63-high = "5000-high"
;
value educ_f
Low-0 = "Low-No"
1-2 = "1_8"
3-6 = "9_12"
7-9 = "C1_C3"
10-high ="C4_High"
;
value VEHICLES_f
low -0 = "Low_0"
1="1"
2="2"
3="3"
4-high="4_High"
;
value bedrooms_f
low-0 = "Low_0"
1="1"
2="2"
3="3"
4="4"
5-high="5_high"
;
value ROOMS_f
Low-0 = "Low_0"
1 = "1"
2 = "2"
03 = "3"
04 = "4"
05 = "5"
06 = "6"
07 = "7"
08 = "8"
9-high = "9-High"
;
value NCHILD_f
Low-0 = "Low_0"
1 = "1"
2 = "2"
3 = "3"
4 = "4"
5-high = "5_High"
;
run;
/** Household type variables **************/
%macro dyear;
%do i=2005 %to 2015;
data householda_&i.;
set ipums.full_2001_2015( where=(pernum=1 and year=&i.));
if gq = 1 or gq=2;
/*******create categories *****/
if hispan = 0 then his=0; **not hispanic**;
else his=1; **hispanic**;
if race = 2 and his=0 then race1="B"; **black**;
else if race = 1 and his=0 then race1="W"; **white**;
else if his=1 then race1="H";
else race1="O";
age1=put(age, age_f.);
age2=put(age, age_ff.);
BPL1=BPL;
if BPL>57 then BPL1=57;
nfams1=put(nfams, family_f.);
NCHILD1=put(NCHILD, NCHILD_f.);
ncouples1=put(ncouples, couple_f.);
NSUBFAM1= put(NSUBFAM, subfamily_f.);
proptx1= put(proptx99, proptx_f.);
EDUC1 =put(EDUC, educ_f.);
VEHICLES1=put(VEHICLES,VEHICLES_f.);
BUILTYR1 = put(BUILTYR2, builtyr_f.);
BEDROOMS1 = put(BEDROOMS, BEDROOMS_f.);
ROOMS1 = put(ROOMS, ROOMS_f.);
sex_HH = sex;
age1_HH = age1;
race1_HH = race1;
citizen_HH = citizen ;
EDUC_HH = EDUC;
EDUC1_HH = EDUC1;
EMPSTAT_HH = EMPSTAT;
age2_HH =age2;
age_HH= age;
/***clear numerical variables ****/
if CONDOFEE <3 then CONDOFEE = .;
if COSTELEC <3 or COSTELEC >9990 then COSTELEC=.;
if COSTGAS <3 or COSTGAS >9990 then COSTGAS=.;
if COSTWATR <3 or COSTWATR >9990 then COSTWATR=.;
if MORTAMT1 <3 then MORTAMT1=.;
if MORTAMT2 <3 then MORTAMT2=.;
if OWNCOST <3 or OWNCOST >9990 then OWNCOST=.;
if PROPINSR <3 then PROPINSR=.;
if RENT <3 or RENT >9990 then RENT=.;
if RENTGRS <3 or RENTGRS >9990 then RENTGRS=.;
if VALUEH <3 or VALUEH >9999990 then VALUEH=.;
state = statefip;
keep year STATE PUMA hhwt &cat_household. &nume_household.
&CROSS_CAT_HOUSEHOLD.
;
run;
%end;
%mend;
%dyear;
/**************************
crosswalk tables here
************************/
%ss_cross_a;
%ss_cross_b;
/**************************************
&datas.: get stsate level results -----cat. variables
**************************************/
/*****macro for calculation ******/
%macro m1 (varlist);
%local i next ;
%let i=1;
%do i=1 %to %sysfunc(countw(&varlist));
%let next=%scan(&varlist,&i);
proc freq data=&datas._&year. noprint ;
by &geo_level.;
weight afact;
tables &next./ out = &next._num;
run;
data &next._num_1;
set &next._num;
keep &geo_level. &next. count;
run;
proc transpose data=&next._num_1 out=&next._wide prefix=&next._;
by &geo_level.;
id &next.;
var count;
run;
data &next._&year.;
set &next._wide;
keep &geo_level. &next._:;
run;
proc sort data=&next._&year.; by &geo_level.; run;
%if &i=1 %then %do;
data bb_cat_&year._&datas.;
set &next._&year.;
run;
%end;
%else %do;
data bb_cat_&year._&datas.;
merge bb_cat_&year._&datas.(in=a) &next._&year.(in=b);
by &geo_level.;
run;
%end;
/*****variable list *****/
proc freq data=&datas._&year. noprint ;
tables &next./ out = &next._list;
by year;
run;
data &next._list1;
set &next._list ( rename = ( &next. = code));
code1 = strip( put(code, $10.));
variable_name = cats ("&next.",'_',code1);
variable1 = cats ("&next.");
keep year variable_name variable1 code1;
run;
proc append base = var_list_&year.
data = &next._list1
force;
run;
;
%end;
%mend m1;
%macro dcal;
%do year=2005 %to 2015;
proc delete data =var_list_&year.; run;
proc sort data= &datas._&year.;
by &geo_level.;
run;
%m1(&cat_&datas..);
proc sort data= bb_cat_&year._&datas.;
by &geo_level.;
run;
data var_list_&year.;
set var_list_&year.;
y&year._code ="X";
keep year variable_name variable1 code1 y&year._code;
;
run;
proc sort data=var_list_&year.;
by variable_name ;
run;
%end;
%mend;
%dcal;
data aa_var_list_&datas.;
merge var_list_2005 - var_list_2015;
by variable_name;
run;
/*****macro for cross-tab calculation ******/
%macro twoway(varlist);
%local i j;
%do i=1 %to %sysfunc(countw(&varlist,%str( )))-1;
%do j=&i+1 %to %sysfunc(countw(&varlist,%str( )));
%let next1=%scan(&varlist,&i);
%let next2=%scan(&varlist,&j);
proc sort data = &datas._&year.;
by &geo_level.;
run;
proc freq data=&datas._&year. noprint;
by &geo_level.;
weight afact;
tables &next1.*&next2./ out = numa;
run;
data num_1;
set numa;
code1 = strip( put(&next1., $10.));
code2 = strip( put(&next2., $10.));
cat_name = cats ("&next1.",'_',code1,'_', "&next2.",'_',code2);
keep &geo_level. cat_name count;
run;
proc transpose data=num_1 out=wide ;
by &geo_level.;
id cat_name;
var count;
run;
data &next1._&next2._&year.;
set wide;
drop _name_ _label_;
run;
proc sort data=&next1._&next2._&year.; by &geo_level.; run;
%if &i=1 %then %do;
data bb_cat_cross_&year._&datas.;
set &next1._&next2._&year.;
run;
%end;
%else %do;
data bb_cat_cross_&year._&datas.;
merge bb_cat_cross_&year._&datas.(in=a) &next1._&next2._&year.(in=b);
by &geo_level.;
run;
%end;
/*****variable list *****/
proc freq data=&datas._&year. noprint ;
tables &next1.*&next2./ out = list;
by year;
run;
data list1;
set list;
variable1 = cats ("&next1.");
code1 = strip( put(&next1., $10.));
variable2 = cats ("&next2.");
code2 = strip( put(&next2., $10.));
variable_name = cats ("&next1.",'_',code1,'_', "&next2.",'_',code2);
keep year variable_name code1 code2 variable1 variable2;
run;
proc append base = var_cross_list_&year.
data = list1
force;
run;
%end;
%end;
%mend;
%macro dcal;
%do year=2005 %to 2015;
proc delete data= var_cross_list_&year.; run;
%twoway(&cat_cross_&datas.1.);
data var_cross_list_&year.;
set var_cross_list_&year.;
y&year._code = "X";
run;
proc sort data=var_cross_list_&year.;
by variable_name ;
run;
%end;
%mend;
%dcal;
data aa_var_cross_list_&datas.;
merge var_cross_list_2005 - var_cross_list_2015;
by variable_name;
drop year;
run;
/**************************************
&datas.: get stsate level results -----numeric variables
above p99 as missing, and below p1 as missing
**************************************/
%macro dcal(varlist);
%do year=2005 %to 2015;
proc sort data = &datas._&year.;
by &geo_level.;
run;
proc means data=&datas._&year. noprint ;
by &geo_level.;
weight afact;
var &varlist.;
output out = num_sum
mean (&varlist.)= p1(&varlist.)= p10(&varlist.)= p25(&varlist.)=
p50(&varlist.)= p75(&varlist.)= p90(&varlist.)= stddev(&varlist.)= /autoname
;
run;
data bb_num_sum1_&year.;
set num_sum;
drop _type_ _freq_;
run;
proc sort data= bb_num_sum1_&year.;
by &geo_level.;
run;
proc contents data=bb_num_sum1_&year. out=num_sum2 noprint;
run;
data var_&year;
set num_sum2;
variable_name = name;
variable1= scan(variable_name, 1, '_');
a=index(variable_name, '_')+1;
code1= substr(variable_name, a);
y&year._code = "X";
if variable_name in( &geolist.) then delete;
keep variable_name y&year._code variable1 code1;
run;
proc sort data=var_&year; by variable_name;
run;
%end;
%mend;
%dcal(&nume_&datas..);
data aa_var_num_list_&datas.;
merge var_2005 - var_2015;
by variable_name;
run;
/**************************************
&datas.: numeric variables * cat. variables
**************************************/
%macro dd(varlist1, varlist2);
%local i j next1 next2;
%do i=1 %to %sysfunc(countw(&varlist1));
%let next1=%scan(&varlist1,&i);
%do j=1 %to %sysfunc(countw(&varlist2));
%let next2=%scan(&varlist2.,&j);
proc sort data = &datas._&year.;
by &geo_level. &next1.;
run;
proc means data=&datas._&year. noprint ;
by &geo_level. &next1.;
weight afact;
var &next2.;
output out = num_sum_&next1._&next2.
mean (&next2.)= &next2.;
run;
proc transpose data=num_sum_&next1._&next2. out=wide
prefix=&next2._&next1._;
by &geo_level.;
id &next1.;
var &next2.;
run;
data &next1._&next2._&year.;
set wide;
drop _name_ _label_;
run;
proc sort data=&next1._&next2._&year.; by &geo_level.; run;
%if &i=1 %then %do;
data bb_cross_num_&year._&datas.;
set &next1._&next2._&year.;
run;
%end;
%else %do;
data bb_cross_num_&year._&datas.;
merge bb_cross_num_&year._&datas.(in=a) &next1._&next2._&year.(in=b);
by &geo_level.;
run;
%end;
proc sort data=bb_cross_num_&year._&datas.;
by &geo_level.;
run;
/*****variable list *****/
proc sort data = &datas._&year.;
by year &next1.;
run;
proc means data=&datas._&year. noprint ;
by year &next1.;
weight afact;
var &next2.;
output out = list
mean (&next2.)= &next2.;
run;
data list1;
set list;
variable1 = cats ("&next1.");
code1 = strip( put(&next1., $10.));
variable2 = cats ("&next2.");
variable_name = cats ("&next2.",'_',"&next1.",'_',code1);
code2="Mean";
keep year variable_name code1 variable1 variable2 code2;
run;
proc append base = cross_num_list_&year.
data = list1
force;
run;
%end;
%end;
%mend ;
%macro dcal;
%do year=2005 %to 2015;
proc delete data= cross_num_list_&year.; run;
%dd(&CAT_CROSS_&datas.1., &cross_num_&datas..);
data cross_num_list_&year.;
set cross_num_list_&year.;
y&year._code ="X";
keep variable_name y&year._code code1 variable2 variable1 code2;
run;
proc sort data=cross_num_list_&year.;
by variable_name ;
run;
%end;
%mend;
%dcal;
data aa_var_cross_num_list_&datas.;
merge cross_num_list_2005 - cross_num_list_2015;
by variable_name;
run;
%macro dcal;
proc delete data =sloan.&datas._&lastgeo._f; run;
proc delete data=sloan.&datas._&lastgeo._var; run;
data sloan.&datas._&lastgeo._f;
merge bb_:;
by &geo_level.;
run;
proc append data=Aa_var_cross_list_&datas.
base=sloan.&datas._&lastgeo._var
force;
run;
proc append data=Aa_var_cross_num_list_&datas.
base=sloan.&datas._&lastgeo._var
force;
run;
proc append data=Aa_var_list_&datas.
base=sloan.&datas._&lastgeo._var
force;
run;
proc append data=Aa_var_num_list_&datas.
base=sloan.&datas._&lastgeo._var
force;
run;
%mend;
%dcal;