Reading External Data

 

*--- SAS program: READING_EXTERNAL_DATA.SAS ;

 

*---Reading external ASCII(TEXT,FLAT) file;

*---Suppose you have a ASCII file 'named state_pop.txt' under c:\temp folder;

*---The following DATA step creates a SAS data set statepop;

 

data statepop;

  infile "c:\temp\state_pop.txt" ;

  input State $ CityPop_80 CityPop_90 NonCityPop_80 NonCityPop_90 Region @@;

  label citypop_80   ='1980 metropolitan pop in millions'

        noncitypop_80='1980 nonmetropolitan pop in millions'

        citypop_90   ='1990 metropolitan pop in millions'

        noncitypop_90='1990 nonmetropolitan pop in million'

        region       ='Geographic region';

run;

 

 

proc print data=statepop noobs label;

  title "City and Non-City Population By State";

run;

 

 

Note

 

  • INFILE statement identifies an external file that want to read with an INPUT statement.
  • LABEL statement associates descriptive labels with variables.
  • NOOBS option suppresses printing the observation number on SAS ouput.
  • LABEL option prints the descriptive label of the variables instead of variable names.

 

 

SAS Output

 

            City and Non-City Population By State

 

                                 Non        Non

            City      City       City       City

  State    Pop_80    Pop_90     Pop_80     Pop_90    Regi

 

   ME       0.405     0.443     0.721      0.785        1

   NH       0.535     0.659     0.386      0.450        1

   VT       0.133     0.152     0.378      0.411        1

   MA       5.530     5.788     0.207      0.229        1

   RI       0.886     0.938     0.061      0.065        1

   CT       2.982     3.148     0.126      0.140        1

   NY      16.144    16.515     1.414      1.475        1

   NJ       7.365     7.730      A          A           1

   PA      10.067    10.083     1.798      1.799        1

   DE       0.496     0.553     0.098      0.113        2

   MD       3.920     4.439     0.297      0.343        2

   DC       0.638     0.607      .          .           2

   VA       3.966     4.773     1.381      1.414        2

   WV       0.796     0.748     1.155      1.045        2

   NC       3.749     4.376     2.131      2.253        2

   SC       2.114     2.423     1.006      1.064        2

   GA       3.507     4.352     1.956      2.127        2

   FL       9.039    12.023     0.708      0.915        2

   KY       1.735     1.780     1.925      1.906        2

   TN       3.045     3.298     1.546      1.579        2

   AL       2.560     2.710     1.334      1.331        2

   MS       0.716     0.776     1.805      1.798        2

   AR       0.963     1.040     1.323      1.311        2

   LA       3.125     3.160     1.082      1.060        2

   OK       1.724     1.870     1.301      1.276        2

   TX      11.539    14.166     2.686      2.821        2

   OH       8.791     8.826     2.007      2.021        3

   IN       3.885     3.962     1.605      1.582        3

   IL       9.461     9.574     1.967      1.857        3

   MI       7.719     7.698     1.543      1.598        3

   WI       3.176     3.331     1.530      1.561        3

   MN       2.674     3.011     1.402      1.364        3

   IA       1.198     1.200     1.716      1.577        3

   MO       3.314     3.491     1.603      1.626        3

   ND       0.234     0.257     0.418      0.381        3

   SD       0.194     0.221     0.497      0.475        3

   NE       0.728     0.787     0.842      0.791        3

   KS       1.184     1.333     1.180      1.145        3

   MT       0.189     0.191     0.598      0.608        4

   ID       0.257     0.296     0.687      0.711        4

   WY       0.141     0.134     0.329      0.319        4

   CO       2.326     2.686     0.563      0.608        4

   NM       0.675     0.842     0.628      0.673        4

   AZ       2.264     3.106     0.453      0.559        4

   UT       1.128     1.336     0.333      0.387        4

   NV       0.666     1.014     0.135      0.183        4

   WA       3.366     4.036     0.776      0.830        4

   OR       1.799     1.985     0.834      0.858        4

   CA      22.907    28.799     0.760      0.961        4

   AK       0.174     0.226     0.227      0.324        4

   HI       0.763     0.836     0.202      0.272        4

 

 

The External File state_pop.txt used in this example

(you may save it as state_pop to your hard drive and run the above program)

 

ME    .405    .443   .721   .785  1   NH   .535    .659   .386   .450  1

VT    .133    .152   .378   .411  1   MA  5.530   5.788   .207   .229  1

RI    .886    .938   .061   .065  1   CT  2.982   3.148   .126   .140  1

NY  16.144  16.515  1.414  1.475  1   NJ  7.365   7.730    .A     .A   1

PA  10.067  10.083  1.798  1.799  1   DE   .496    .553   .098   .113  2

MD   3.920   4.439   .297   .343  2   DC   .638    .607      .     .   2

VA   3.966   4.773  1.381  1.414  2   WV   .796    .748  1.155  1.045  2

NC   3.749   4.376  2.131  2.253  2   SC  2.114   2.423  1.006  1.064  2

GA   3.507   4.352  1.956  2.127  2   FL  9.039  12.023   .708   .915  2

KY   1.735   1.780  1.925  1.906  2   TN  3.045   3.298  1.546  1.579  2

AL   2.560   2.710  1.334  1.331  2   MS   .716    .776  1.805  1.798  2

AR    .963   1.040  1.323  1.311  2   LA  3.125   3.160  1.082  1.060  2

OK   1.724   1.870  1.301  1.276  2   TX 11.539  14.166  2.686  2.821  2

OH   8.791   8.826  2.007  2.021  3   IN  3.885   3.962  1.605  1.582  3

IL   9.461   9.574  1.967  1.857  3   MI  7.719   7.698  1.543  1.598  3

WI   3.176   3.331  1.530  1.561  3   MN  2.674   3.011  1.402  1.364  3

IA   1.198   1.200  1.716  1.577  3   MO  3.314   3.491  1.603  1.626  3

ND    .234    .257   .418   .381  3   SD   .194    .221   .497   .475  3

NE    .728    .787   .842   .791  3   KS  1.184   1.333  1.180  1.145  3

MT    .189    .191   .598   .608  4   ID   .257    .296   .687   .711  4

WY    .141    .134   .329   .319  4   CO  2.326   2.686   .563   .608  4

NM    .675    .842   .628   .673  4   AZ  2.264   3.106   .453   .559  4

UT   1.128   1.336   .333   .387  4   NV   .666   1.014   .135   .183  4

WA   3.366   4.036   .776   .830  4   OR  1.799   1.985   .834   .858  4

CA  22.907  28.799   .760   .961  4   AK   .174    .226   .227   .324  4

HI    .763    .836   .202   .272  4