Return to Homepage

Goto the Tip of the Month Archive

Other interesting pages ...
LinkedIn Profile
SAS Cheat Sheet
Useful SAS Code
Full SAS Example
Basic Statistics
Contact Information

SAS Tip of the Month
November 2010
(for SAS and WPS)

This month we continue from last month by making a PROC PRINT output look better than just a "data dump", concentrating on the BY and ID options.

Lets look at our program:

   === PROGRAM ===
   ** Our data;
   data SheepNumbers;
      length Country $25 Year Number 8;
      infile cards;
      input Country $ & Year Number;
   cards;
   Australia  2007 85711187
   Australia  2006 91028408
   Australia  2005 101124891
   Canada  2007 879100
   Canada  2006 893800
   Canada  2005 977600
   China  2007 146018203
   China  2006 151337202
   China  2005 152035223
   India  2007 64269000
   India  2006 63558000
   India  2005 62854000
   New Zealand  2007 38460477
   New Zealand  2006 40098191
   New Zealand  2005 39879660
   South Africa  2007 25082100
   South Africa  2006 24982996
   South Africa  2005 25334000
   United Kingdom  2007 33946000
   United Kingdom  2006 34722000
   United Kingdom  2005 35253048
   United States of America  2007 6165000
   United States of America  2006 6230000
   United States of America  2005 6135000
   ;
   run;

   ** Set up standard titles and footnotes;
   title1 "Sheep Numbers in Select Countries, By Year";
   footnote1 "Note: Numbers for India are FAO Estimate";
   footnote2 "Source: UNData, 03Nov2010";

   ** Sort our data for the PRINT procedure. Note use of
      DESCENDING option to sort in decreasing year value.;
   proc sort data=SheepNumbers;
      by Country DESCENDING Year Number;
   run;

   ** Print out a basic lising of the data -- does not
      look very nice, just a "data dump";
   proc print data=SheepNumbers;
      var Country Year Number;
      title2 "(Basic Output)";
   run;

   ** Now something that looks good;
   proc print data=SheepNumbers label;
      by Country;
      id Country;  ** Acts like a "GROUPING" variable;
      var Year Number;
      format Number comma12.;
      label Country='Country'
            Year='Year'
            Number='Repported Number';
      title2 "(A Better Looking Output)";
   run;

The output is:

   === OUTPUT ===
           Sheep Numbers in Select Countries, By Year
                         (Basic Output)

      Obs    Country                     Year      Number

        1    Australia                   2007     85711187
        2    Australia                   2006     91028408
        3    Australia                   2005    101124891
        4    Canada                      2007       879100
        5    Canada                      2006       893800
        6    Canada                      2005       977600
        7    China                       2007    146018203
        8    China                       2006    151337202
        9    China                       2005    152035223
       10    India                       2007     64269000
       11    India                       2006     63558000
       12    India                       2005     62854000
       13    New Zealand                 2007     38460477
       14    New Zealand                 2006     40098191
       15    New Zealand                 2005     39879660
       16    South Africa                2007     25082100
       17    South Africa                2006     24982996
       18    South Africa                2005     25334000
       19    United Kingdom              2007     33946000
       20    United Kingdom              2006     34722000
       21    United Kingdom              2005     35253048
       22    United States of America    2007      6165000
       23    United States of America    2006      6230000
       24    United States of America    2005      6135000

            Note: Numbers for India are FAO Estimate
                   Source: UNData, 03Nov2010
  ------------------------------------------------------------

           Sheep Numbers in Select Countries, By Year
                   (A Better Looking Output)

                                               Repported
                Country             Year          Number

        Australia                   2007      85,711,187
                                    2006      91,028,408
                                    2005     101,124,891

        Canada                      2007         879,100
                                    2006         893,800
                                    2005         977,600

        China                       2007     146,018,203
                                    2006     151,337,202
                                    2005     152,035,223

        India                       2007      64,269,000
                                    2006      63,558,000
                                    2005      62,854,000

        New Zealand                 2007      38,460,477
                                    2006      40,098,191
                                    2005      39,879,660

        South Africa                2007      25,082,100
                                    2006      24,982,996
                                    2005      25,334,000

        United Kingdom              2007      33,946,000
                                    2006      34,722,000
                                    2005      35,253,048

        United States of America    2007       6,165,000
                                    2006       6,230,000
                                    2005       6,135,000

            Note: Numbers for India are FAO Estimate
                   Source: UNData, 03Nov2010

In the second PROC PRINT call, a NOOBS is implied so that option is not required. The variable in the ID statement is used as a "grouping" variable and must also be included in the BY statement -- this is what makes the COUNTRY variable appear as a grouping variable and have a spacing (blank line) between each group. The format COMMA12. is used to make the number appear in comma format so that it is easier to read. As with last month, the LABEL statement and option is used to make user friendly labels appear rather than the variable names. A lot is here so copy the program and try it out.

Will be presenting two papers at NESUG (North East SAS Users Group) conference in Baltimore, Maryland later this month -- if you are there come and say 'hi'.

See you in December.

________________________________
Updated November 3, 2010