Example: USA_population.sas

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      = '4/1/2000 resident Census 2000 population'

        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

 

                                                              United States Population                                                              1

                                                                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

 

                         ALABAMA                    4,447,100       4,447,100       4,451,975       4,468,912       4,486,508

                         ALASKA                       626,932         626,931         627,697         633,630         643,786

                         ARIZONA                    5,130,632       5,130,632       5,167,142       5,306,966       5,456,453

                         ARKANSAS                   2,673,400       2,673,398       2,678,668       2,694,698       2,710,079

                         CALIFORNIA                33,871,648      33,871,648      34,010,375      34,600,463      35,116,033

                         COLORADO                   4,301,261       4,301,331       4,326,758       4,430,989       4,506,542

                         CONNECTICUT                3,405,565       3,405,565       3,411,956       3,434,602       3,460,503

                         DELAWARE                     783,600         783,600         786,512         796,599         807,385

                         DISTRICT OF COLUMBIA         572,059         572,059         571,641         573,822         570,898

                         FLORIDA                   15,982,378      15,982,400      16,051,395      16,373,330      16,713,149

                         GEORGIA                    8,186,453       8,186,486       8,234,373       8,405,677       8,560,310

                         HAWAII                     1,211,537       1,211,537       1,212,670       1,227,024       1,244,898

                         IDAHO                      1,293,953       1,293,953       1,299,721       1,320,585       1,341,131

                         ILLINOIS                  12,419,293      12,419,296      12,440,846      12,520,227      12,600,620

                         INDIANA                    6,080,485       6,080,485       6,091,950       6,126,743       6,159,068

                         IOWA                       2,926,324       2,926,327       2,928,742       2,931,967       2,936,760

                         KANSAS                     2,688,418       2,688,418       2,692,557       2,702,125       2,715,884

                         KENTUCKY                   4,041,769       4,042,209       4,048,832       4,068,816       4,092,891

                         LOUISIANA                  4,468,976       4,468,979       4,469,768       4,470,368       4,482,646

                         MAINE                      1,274,923       1,274,923       1,277,284       1,284,470       1,294,464

                         MARYLAND                   5,296,486       5,296,483       5,312,461       5,386,079       5,458,137

                         MASSACHUSETTS              6,349,097       6,349,097       6,361,720       6,401,164       6,427,801

                         MICHIGAN                   9,938,444       9,938,444       9,956,115      10,006,266      10,050,446

                         MINNESOTA                  4,919,479       4,919,479       4,934,248       4,984,535       5,019,720

                         MISSISSIPPI                2,844,658       2,844,658       2,848,829       2,859,733       2,871,782

                         MISSOURI                   5,595,211       5,595,211       5,605,067       5,637,309       5,672,579

                         MONTANA                      902,195         902,195         903,416         905,382         909,453

                         NEBRASKA                   1,711,263       1,711,263       1,713,375       1,720,039       1,729,180

                         NEVADA                     1,998,257       1,998,257       2,018,828       2,097,722       2,173,491

                         NEW HAMPSHIRE              1,235,786       1,235,786       1,240,472       1,259,359       1,275,056

                         NEW JERSEY                 8,414,350       8,414,350       8,433,276       8,511,116       8,590,300

                         NEW MEXICO                 1,819,046       1,819,046       1,821,767       1,830,935       1,855,059

                         NEW YORK                  18,976,457      18,976,457      18,999,760      19,084,350      19,157,532

                         NORTH CAROLINA             8,049,313       8,049,474       8,082,261       8,206,105       8,320,146

                         NORTH DAKOTA                 642,200         642,200         641,131         636,550         634,110

                         OHIO                      11,353,140      11,353,008      11,363,568      11,389,785      11,421,267

                         OKLAHOMA                   3,450,654       3,450,656       3,454,408       3,469,577       3,493,714

                         OREGON                     3,421,399       3,421,405       3,431,137       3,473,441       3,521,515

                         PENNSYLVANIA              12,281,054      12,281,054      12,286,107      12,303,104      12,335,091

                         RHODE ISLAND               1,048,319       1,048,319       1,050,698       1,059,659       1,069,725

                         SOUTH CAROLINA             4,012,012       4,012,010       4,023,725       4,062,125       4,107,183

                         SOUTH DAKOTA                 754,844         754,844         755,783         758,324         761,063

                         TENNESSEE                  5,689,283       5,689,277       5,703,246       5,749,398       5,797,289

                         TEXAS                     20,851,820      20,851,812      20,955,248      21,370,983      21,779,893

                         UTAH                       2,233,169       2,233,169       2,243,406       2,278,712       2,316,256

                         VERMONT                      608,827         608,827         609,952         612,978         616,592

                         VIRGINIA                   7,078,515       7,078,499       7,105,900       7,196,750       7,293,542

                         WASHINGTON                 5,894,121       5,894,119       5,911,803       5,993,390       6,068,996

                         WEST VIRGINIA              1,808,344       1,808,350       1,807,326       1,800,975       1,801,873

                         WISCONSIN                  5,363,675       5,363,701       5,374,367       5,405,947       5,441,196

                         WYOMING                      493,782         493,782         494,086         493,754         498,703

                                                 ============    ============    ============    ============    ============

                                               281,421,906     281,422,509     282,224,348     285,317,559     288,368,698

 

 

 

 

                                             Tugluke Abdurazak email: tugluke.abdurazak@us.pwcglobal.com


 

                                                               United States Population                                                              2

                                                               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.