This is an example of complete SAS program doing the following:
1. Reading a text (ASCII) file to SAS;
2. Generate formats using PROC FORMAT;
3. Summarize SAS data set using PROC SUMMARY;
4. Print by using PROC PRINT and _TYPE_;
5. Output HTML file using Output Delivery System (ODS);
6. Create maps using PROC GMAP.
Note: You may download the US population data file ‘usstate.txt’
from http://eire.census.gov/popest/estimates_dataset.php
(go to ‘State estimates by demographic characteristics -
single year of age, sex, race, and Hispanic Origin’ and download All States
(4.9M) and save it as ‘usstate.txt’ to your hard
drive). The variables( in the order it appears in the file ) in ussstate.txt
are state_fips_code,
sex, ethnic_origin, race, age, cesus2000pop, estimatesbase2000, popestimate2000,
popestimate2001, popestimate2002. The following is a sample of records:
01 1 1 1 0 19163
19163 19473 19606
19459
01 2 1 1 0 18034
18034 18342 18503
18399
01 1 1 2 0 9148
9148 9054 10382
10326
01 2 1 2 0 8957
8957 8836 10224
10055
01 1 1 3 0 128
128 128 56
58
01 2 1 3 0 110
110 109 76
76
01 1 1 4 0 221
221 227 202
205
01 2 1 4 0 227
227 242 208
208
01 1 1 5 0 6
6 9 6
6
01 2 1 5 0 9
9 10 7
7
01 1 1 6 0 539
539 494 434
444
01 2 1 6 0 526
526 460 412
414
01 1 2 1 0 866
866 872 969
960
01 2 2 1 0 869
869 875 946
930
(more data lines)
SAS Program: USA_population.sas
/******************************************************************************************/
/* program: usa_population.sas */
/* purpose: This is an example of using SAS to */
/* 1. Read text file;
*/
/* 2. Generate format with PROC
FORMAT;
*/
/* 3. Create summary statistics with
PROC SUMMARY;
*/
/* 4. Print by using _TYPE_
statement;
*/
/* 5. Output HTML file using
ODS(Output Delivery System).
*/
/* 6. Create maps using PROC
GMAP.
*/
/******************************************************************************************/
options ls=150 ps=45 mprint nodate pageno=1;
title 'United
States Population';
footnote "Tugluke
Abdurazak email: tugluke.abdurazak@us.pwcglobal.com";
*---1. Read text file;
data uspop;
infile "c:\temp\usstate.txt";
input state_fips_code
sex
ethnic_origin
race
age
cesus2000pop
estimatesbase2000
popestimate2000
popestimate2001
popestimate2002 ;
*---Assign
label to the variables;
label state_fips_code
= 'State Fips Code'
ethnic_origin = 'Ethnic
Origin'
cesus2000pop = '
estimatesbase2000 = '4/1/2000 resident population estimates base'
popestimate2000 = '7/1/2000 resident population estimate'
popestimate2001 = '7/1/2001 resident population estimate'
popestimate2002 = '7/1/2002 resident population estimate';
*---Convert
fips code to state name;
state_name = fipname(state_fips_code);
run;
*---2. Generate format;
proc format;
value sex
1 = 'Male'
2 = 'Female'
;
value eth
1 = 'Non-Hispanic
or Latino Origin'
2 = 'Hispanic
or Latino Origin'
;
value race
1 = 'White
Alone'
2 = 'Black
Alone'
3 = 'American
Indian or Alaskan Native Alone'
4 = 'Asian
Alone'
5 = 'Native
Hawaiian and Other Pacific Islanders Alone'
6 = 'Two or
More Race Groups'
;
run;
*---3. Create summary
statistics;
proc summary data=uspop;
class state_name sex ethnic_origin race ;
var cesus2000pop
estimatesbase2000
popestimate2000
popestimate2001
popestimate2002 ;
format sex sex. ethnic_origin eth. race race.;
output out=smry sum=cesus2000pop
estimatesbase2000
popestimate2000
popestimate2001
popestimate2002 ;
run;
*---4. Print by using
_TYPE_ statement;
proc print data=smry label split=' ' noobs;
where _type_ = 8;
title2 "US
Population by State";
var state_name cesus2000pop estimatesbase2000
popestimate2000 popestimate2001 popestimate2002;
format cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002 comma12.;
sum cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002;
run;
proc print data=smry label split=' ' noobs;
where _type_ = 4;
title2 "US
Population by Gender";
var sex cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002;
format cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002 comma12.;
sum cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002;
run;
*---5. Output HTML file
using ODS;
ods html file = "c:\temp\uspop_by_state.html";
proc print data=smry label split=' ' noobs;
where _type_ = 8;
title2 "US
Population by State";
var state_name cesus2000pop estimatesbase2000
popestimate2000 popestimate2001 popestimate2002;
format cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002 comma12.;
sum cesus2000pop estimatesbase2000 popestimate2000
popestimate2001 popestimate2002;
run;
ods html close;
*---6. Create maps using
PROC GMAP;
*---Set options;
goptions reset=global gunit=pct border cback=white
colors=(blue green lime lipk cyan red yellow brown )
ctext=black ftext=swiss htitle=6 htext=3;
*---Get map data;
data us;
set maps.us;
rename state = state_fips_code;
run;
*---Map it;
title1 'Population
by State';
footnote1 j=r 'Tugluke
Abdurazak';
proc gmap map=us data=uspop ;
id state_fips_code;
prism popestimate2002 / coutline=gray ;
format popestimate2002 comma12.;
run;
quit;
SAS Output
US Population by State
4/1/2000 4/1/2000
resident resident 7/1/2000 7/1/2001 7/1/2002
Census population resident resident resident
2000 estimates population population population
state_name population base estimate estimate estimate
NEW
RHODE ISLAND 1,048,319 1,048,319 1,050,698 1,059,659 1,069,725
============ ============ ============ ============ ============
281,421,906 281,422,509 282,224,348 285,317,559 288,368,698
Tugluke Abdurazak email: tugluke.abdurazak@us.pwcglobal.com
US Population by Gender
4/1/2000 4/1/2000
resident resident 7/1/2000 7/1/2001 7/1/2002
Census population resident resident resident
2000 estimates population population population
sex population base estimate estimate estimate
Male 138,053,563 138,053,863 138,469,654 140,075,610 141,660,978
Female 143,368,343 143,368,646 143,754,694 145,241,949 146,707,720
============ ============ ============ ============ ============
281,421,906 281,422,509 282,224,348 285,317,559 288,368,698
Tugluke Abdurazak email: tugluke.abdurazak@us.pwcglobal.com
HTML file from part 5.
Click > USA population
US Population Map form part 6.
