proc datasets

摘录:《PROC DATASETS;The Swiss Army Knife of SAS® Procedures》_Michael A. Raithel, Westat, Rockville, MD

 

There are many ways that one could go about organizing the functions provided by PROC DATASETS. The way that
this paper is organized is to divide the DATASETS procedure’s functionality into four main categories:

1. Obtaining SAS Library Information.

The CONTENTS statement provides you with the means to list the files in a SAS library and determine their characteristics. Executing the CONTENTS statement is a good starting point for understanding the nature of the files in a SAS library before considering how you might modify them.

例:contents 语句

proc datasets library=sgflib;
     contents data=bweight details varnum memtype=data;
run;
quit;

2. Modifying Attributes of SAS Variables.

This PROC DATASETS capability allows you to make changes to SAS data set metadata at very little cost in terms of computer resources. This is one of the more popular uses for the DATASETS procedure, and one that you will definitely want to have in your SAS toolkit.

例:format,informat,label,rename等语句

proc datasets library=sgflib;
modify snacks;
            format price dollar6.2
                       date worddate.
                        ;
            informat date mmddyy10.;
            label product = "Snack Name"
                     date = "Sale Date"
                       ;
            rename Holiday = Holiday_Sale;
run;
quit;
例:attrib语句

proc datasets library=sgflib;
modify snacks;
           attrib _all_ format=;
           attrib _all_ informat=;
           attrib _all_ label="";

run;

quit;

3. Modifying Attributes of SAS Data Sets.

This group of PROC DATASETS statements allows you to perform tasks that directly affect the structure and functionality of SAS data sets. Many of these statements involve more advanced data set structures, so you may not find yourself using them very often. However, you should be aware that the DATASETS procedure can perform these tasks when you need to accomplish them in your SAS programs. You can use these statements to:

.Concatenate SAS data sets using the APPEND statement

例:

proc datasets library=sgflib;
append base=sgflib.snacks
            data=sgflib.snacktran
            appendver=v6 force getsort nowarn;
quit;

.Manage audit trails using the AUDIT statement

例:

/*creating an audit file for sancks sas data set*/

proc datasets library=sgflib nolist;
       audit snacks;
                initiate;
                log admin_image=yes
                     before_image=yes
                     data_image=no
                     error_image=yes;
               user_var update_reason $15;
run;
quit;

/*insert a new row into the sancks table*/

proc sql;
     insert into sgflib.snacks
           set product = 'Snake Snacks',
                qtysold = 20890,
                price = 2.5,
                advertised=0,
                holiday=0,
                date=18379,
                update_reason = "Add new product";
quit;

/*print the resulting audit file entry with SQL precedure*/

options linesize=150;
proc sql;
select product,
           update_reason,
           _atopcode_,
           _atuserid_ format=$9.,
           _atdatetime_
from sgflib.snacks(type=audit);
quit;


.Manage integrity constraints using the IC statements

/*creating integrity constraints for the SHOES SAS data*/
proc datasets library=sgflib nolist;
modify shoeregions;
             ic create primkey = primary key (region);
run;
modify shoes;
             ic create pkey = primary key (sequenceno);
             ic create regprodsub = distinct (region product subsidiary)
                           message = "Region, Product, Subsidiary combination must be unique";
             ic create storelimit = check(where=(stores < 50))
                           message = "Limit of 50 stores";
             ic create returnsales = check(where=(returns+sales < inventory))
                           message = "Returns + Sales cannot exceed Inventory";
             ic create fkey = foreign key (region) references sgflib.shoeregions
                          on update cascade on delete set null;
run;
quit;

/*removing integrity constraints*/

proc datasets library=sgflib nolist;
modify shoes;
ic delete _all_;
run;
modify shoeregions;
ic delete primkey;
run;
quit;

/*reactivating integrity constraints*/

proc datasets library=sgflib nolist;
   modify shoes;
         ic reactivate fkey references sgflib;
   run;
quit;
.Manage indexes using the index statements

 

/*creating indexes*/

proc datasets library=sgflib nolist;
        modify shoes;
        index create sequenceno / unique updatecentiles=always;
        index create reg_sub_prod = (region subsidiary product) / nomiss;
run;

quit;

/*delete indexes*/

proc datasets library=sgflib nolist;
modify shoes;
index delete sequenceno reg_sub_prod;
run;
quit;

/*managing centiles for indexes*/

proc datasets library=sgflib nolist;
modify shoes;
index centiles sequenceno / refresh;
index centiles reg_sub_prod / updatecentiles=20;
run;
quit;


.Change file attributes using the MODIFY statement

/*changing data set attributes*/

proc datasets library=sgflib nolist;
modify shoes(label = "Shoe Sales for First Quarter of 2009" sortedby = region) /
                                   correctencoding = wlatin1 dtc = "31MAR09:07:45:00"dt;
run;
quit;

/*modifying passwords*/

proc datasets library=sgflib nolist;

         /* Assign passwords */
modify shoes(alter=rock read=paper write=scissors);
modify snacks(pw=skynet);
          /* Alter passwords */
modify shoes(alter=rock/hamlet read=paper/macbeth write=scissors/othello);
modify snacks(pw=skynet/cyberdyn);
         /* Remove passwords */
modify shoes(alter=hamlet/ read=macbeth/ write=othello/);
modify snacks(pw=cyberdyn/);
run;
quit;

/*modifying generation groups*/

   /*specify the generations using GENNUM option */

proc print data=shoes(gennum=2);
run;

   /*changing the number of generation groups*/

proc datasets library=sgflib nolist;
modify shoes(genmax=10);
modify snacks(genmax=0);
run;
quit;

.Recover indexes and integrity constraints using the REBUILD statement

例:

proc datasets library=scratlib nolist;
rebuild shoes;
run;
quit;

 

4. Managing Files in SAS Libraries.

This collection of DATASETS procedure statements facilitates the processing of all types of files within SAS data libraries. Some of these actions, such as COPY-ing and DELETE-ing will be very familiar to many SAS programmers because they are widely used. Others, such as EXCHANGE-ing and SAVE-ing, are less frequently used, but are good to have when you need them. This group of DATASETS procedure statements permit you to:

.Cascade file renames using the AGE statement

例:

proc datasets library=work nolist;
age newshoes shoes oldshoes oldestshoes;
run;
quit;

 

.Rename SAS files using the CHANGE statement

例:

proc datasets library=work nolist;
           change bweight = BodyWeight shoes = ShoeSales;
run;
quit;


.Copy files using the COPY, SELECT, and EXCLUDE statements

例:

proc datasets library=work nolist;
copy out=bkuplib clone datecopy;
              select shoesales;
run;
copy out=bkuplib clone datecopy force constraint=yes index=yes move;
            Select bodyweight;
run;
quit;


.Permanently remove files using the DELETE statement

例:

proc datasets library=work nolist;
delete shoes / gennum=hist;
delete bodyweight / memtype=data;
run;
quit;


.Swap file names using the EXCHANGE statement

例:

proc datasets library=work nolist;
exchange shoes = shoes_backup
                snacks = snacks_current;
run;
quit;


.Fix damaged files using the REPAIR statement

例:

proc datasets library=scratlib nolist;
repair shoes;
run;
quit;


.Keep files during a delete operation using the SAVE statement

proc datasets library=work nolist;
save shoes_backup;
run;
quit;

你可能感兴趣的:(proc datasets)