Advanced Topics on SAS Data Step Programing: What are your options?


stats image
Advanced Topics on SAS Data Step Programing: What are
your options?

 

last updated: 2FEB04

line

 

 
 
 

 

 
 
 

System options

 

    Before you start, you might be interested in knowing what options are
    selected as default in your SAS session. If you run:


    PROC options; run;

    you will see in the LOG window a complete listing of what are the options
    you are using in your SAS session. You do not have to worry about many of
    the things you will see displayed, so don’t panic.

    Page set up options.

  • This code…


    options nodate pageno=1 nocenter linesize=120 pagesize=65 FORMCHAR="|----|+|---+=";

  • Results in…

    • Page numbering starting with one
    • width and height of the paper to larger area
    • supress SAS dates
    • supress centering of the output on the output page
    • set a character formatting for lines such that it is easier to copy
      and paste into other programs.
  • This code…


    TITLE1 "Here is my title %SYSFUNC(DATE(), EURDFDE9.) %SYSFUNC(TIME(), TIMEAMPM8.)";

  • Ouputs as:

    Here is my title 02OCT2003 5:07 PM 17:04 Thursday, October 2, 2003 1

     

     
     
     

    DATA step options

     

      The DATA step has two types of options: one which modifies the individual
      datasets, and one which modifies the DATA step itself. We will refer to
      those who make global changes within the DATA step as system
      options
      .


      DATA dataset1 (OPTION =list1) dataset2 (OPTION =list2) /OPTIONS=list;

      RUN;


    1. Debugging your programsSometimes it is convenient to step through the code SAS is processing. The
      Data Step Debugger is a tool that allows you to do this. To invoke it, you
      use the option DEBUG:


      DATA dataset1 /DEBUG; RUN;

      As a result a SOURCE window will appear. This window displays the data
      step code and the actual line of code being executed. The LOG window will
      accept debugger commands. Some of categories examples of debugger
      commands are:

      GO JUMP STEP <– Program execution control
      CALCULATE DESCRIBE EXAMINE SET <– variable manipulation
      BREAK DELETE LIST TRACE WATCH <– debugger request manipulation
      ENTER <– tailoring
      QUIT <– terminate
      HELP SWAP <– window control
    2. Managing Variables [list1 or list2].Each of the following three commands can be used, and/or combined, to
      manage
      varibles in one of three basic ways.

      1. Within a single file
      2. Within Multiple files
      3. Across multiple files

      These commands are:

      KEEP= variable list
      reads/saves only listed
      DROP= variable list
      reads/saves all but listed
      RENAME= (oldname=newname)
      substitutes names of listed

      For Example:

      libname cdir 'c:\temp\';
      DATA myfile1 (DROP = year age) myfile2 (KEEP= index origin RENAME=(ss=social) ;
      set cdir.myinput1;
      run;

      …will read in C:\temp\myinput.ssd and create two files: myfile1
      and myfile2 which will be modified by dropping or keeping variables
      Or, to drop a list of variables from BOTH myfile1 and myfile2, you would
      take advantage of the OUTPUT statement which is implied at the end of
      the data step by typing…


      DATA myfile1 myfile2;
      ...
      	DROP ss year age;
      RUN;
    3. Managing datasets.

      Each of the following commands can be used, and/or combined, to
      manage your datafiles. Some of these commands can be used as individual
      file options or as global system options.

      <!–

      –>

      COMPRESS= [yes | no]
      By default SAS dataset are stored as fixed record lengh. This option is
      optimally used when variable record lenght is used, as to reduce storage
      space.
      REUSE= By default,
      space associated with data deleted within a dataset is not recycled, but
      left empty. This option tells SAS to reuse empty space resulting from
      deleted records.
      $POINTOBS= Used for
      compressed datasets, allows to access a particular record.
      REPLACE= [yes | no] Setting
      this option to NO, provides write protection for that file for
      the duration of the datastep.
      LABEL= ‘dataset label’ Assigns a
      label to be displayed by procedures. This helps is the file name is
      cryptic.
      SORTEDBY= var1 var2 Defines
      variables in the input data which are already sorted and allows for BY
      statement to be used in PROC or DATA steps.
    4. Access control options.

      The following options control file access to SAS datasets. This means
      that you can control who reads/writes to/from your datasets.
      Remember that when passwords are used, forgetting the password means
      ‘bye-bye’ data. All of these commands use the following format.

      libname cdir 'c:\temp\';
      DATA myfile1 (COMMAND = options) ;
      ...
      run;

      PW=xxxxxxxx Sets an read/write
      password for a dataset.
      READ= Password only for READing a
      file.
      WRITE= Password only for
      WRITE’ng a file.
      ALTER= Password only if
      changes are made to the dataset.
      ENCRYPT= [yes | no] Allow
      for encryption of dataset. Useful for protection of sensitive data.
    5. Indexing

      By indexing a SAS dataset a look up table is created which allows you
      point to particular values of the indexing variable. An index can be based
      on a single variable, or on a combination of variables. If a combination
      of variables is desired, you need to give a nickname to that particular
      combination.

      DATA myfile1 (index = var1 ) myfile2 (index= (nickname= var1 var2));
      ...
      run;

    6. Selective OUTPUT of data
      By taking control of the OUTPUT statement, you can create multiple files
      using the same SAS codes and add records to
      each file depending on certain conditions being satisfied. Note that
      this code would require for an explicit output statement for all the
      files being created.

      Here we create two files depending on the value of a variable:

      DATA myfile1 myfile2 ;
      set cdir.myinput1;
      if region='NE' then output myfile1;
      else output myfile2;
      run;

      Here we create two files: one depending on the value of a variable,
      another with all the cases:

      DATA myfile1 myfile2 ;
      set cdir.myinput1;
      if region='NE' then output myfile1;
      output myfile2;
      run;

     

     
     
     

    File transfers

     

    1. Reading in a SAS dataset which was created in another version of
      SAS

      To be able to read in a file which was created in another version of SAS,
      but in the SAME operating system you are intending to use this file in,
      you first need to set up a LIBRARY alias for all files for the older
      version. SAS wants to have files from one version separate from
      files of other SAS versions. In the example below we setup a directory
      where all SAS 6.12 files will be located.

      libname cdirold v612 'C:\SASDATA\data612\';
      libname cdir 'C:\SASDATA\data\';
      

    2. Reading in a SAS dataset which was created in another operating
      system

      To tranfer files across operating systems, you need to save the data in
      the source system to a SAS PORTABLE file format. This format extracts all
      system specific information and leaves the data with data relevant
      information on the header of the file. This PORTABLE file is a binary file
      but smaller than its original version. Upon doing the transfer via FTP or
      the file transfer protocol of your choice, you need to read in the data
      via one of a few methods.
      Also refer to Converting
      to/form SAS files
      for more detailed information on this process.

      libname cdir 'C:\sasdata\newdata\';
      filename trans 'C:\sasdata\oldfile.por';
      proc cimport library=test2 infile=trans; run;
      

     

     
     
     

    Macro Programming

     

     

     
     
     

    Examples

     

     

     
     
     

    References

     

    <!–

  • Basic Methods to Introduce External Data into the
    SAS system

    More [and complex] Examples

    –>

     

     


      2004-2-3  VDC: mailboxWWWSTATS@uic.edu


      UIC Home Page Search UIC Pages Contact UIC