Previous Page | Next Page

Starting with SAS Data Sets

Creating More Than One Data Set in a Single DATA Step

You can use a single DATA step to create more than one data set at a time. You can create data sets with different contents by using the KEEP= or DROP= data set options. For example, the following DATA step creates two SAS data sets: SERVICES contains variables that show services-related expenditures, and ADMIN contains variables that represent the administration-related expenditures. Use the KEEP= option after each data set name in the DATA statement to determine which variables are written to each SAS data set being created.

data services(keep=ServicesTotal ServicesPolice ServicesFire
              ServicesWater_Sewer)
     admin(keep=AdminTotal AdminLabor AdminSupplies
           AdminUtilities);
   set city;
run;

proc print data=services;
   title 'City Expenditures: Services';
run;

proc print data=admin;
   title 'City Expenditures: Administration';
run;

The following output shows both data sets. Note that each data set contains only the variables that are specified with the KEEP= option after its name in the DATA statement.

Creating Two Data Sets in One DATA Step

                          City Expenditures: Services                          1

                                             Services
                     Services    Services     Water_     Services
              Obs     Police       Fire        Sewer       Total

                1      2819        1120         422        4361  
                2      2477        1160         500        4137  
                3      2028        1061         510        3599  
                4      2754         893         540        4187  
                5      2195         963         541        3699  
                6      1877         926         535        3338  
                7      1727        1111         535        3373  
                8      1532        1220         519        3271  
                9      1448        1156         577        3181  
               10      1500        1076         606        3182  
               11      1934         969         646        3549  
               12      2195        1002         643        3840  
               13      2204         964         692        3860  
               14      2175        1144         735        4054  
               15      2556        1341         813        4710  
               16      2026        1380         868        4274  
               17      2526        1454         946        4926  
               18      2027        1486        1043        4556  
               19      2037        1667        1152        4856  
               20      2852        1834        1318        6004  
               21      2787        1701        1317        5805  
                       City Expenditures: Administration                       2

                        Admin      Admin       Admin      Admin
                 Obs    Labor    Supplies    Utilities    Total

                   1     391        63           98        552 
                   2     172        47           70        289 
                   3     269        29           79        377 
                   4     227        21           67        315 
                   5     214        21           59        294 
                   6     198        16           80        294 
                   7     213        27           70        310 
                   8     195        11           69        275 
                   9     225        12           58        295 
                  10     235        19           62        316 
                  11     266        11           63        340 
                  12     256        24           55        335 
                  13     256        28           70        354 
                  14     241        19           83        343 
                  15     238        25           97        360 
                  16     226        24           97        347 
                  17     317        13           89        419 
                  18     226         .           82          . 
                  19     244        20           88        352 
                  20     270        23           74        367 
                  21     307        26           66        399 

Note:   In this case, using the KEEP= data set option is necessary, because when you use the KEEP statement, all data sets that are created in the DATA step contain the same variables.  [cautionend]

Previous Page | Next Page | Top of Page