Lesson 33:  Headings, Do Loops, and Sampling

<<Previous|Contents|Next>>

Video:  Printing Row and Column Headings

More options using the reset command:

    reset autoname;

Autoname gives headings for the rows and columns of matrices that are printed.  Default labels are Row1, Col1, etc.  You can define a vector of headings, such as head={"Mon" "Tue" "Wed"};  Then in a print statement you can put

    print x[colname=head rowname=rowh];

where rowh is another vector of headings for the rows.

The mattrib command provides a more sophisticated way to assign row and column names. 

    mattrib x rowname=(rowh) colname=(head);
    print x;

This association of the row and column headings will last as long as the iml step runs, so you don't have to keep specifying them in the print statement.

Video:  More about ranges

The range assignment like

    a=1:10;
    b=5:2;

actually creates a vector of integers specified by the range.  We saw that in subscripting we can use variables like a and b to specify the rows and columns to be selected.  However, any vector will work, and the numbers need not be in order.  For example, you could have

    a={5, 3, 7, 1};

Using this variable to select rows in a subscript notation would pull the 5th, 3rd, 7th, and 1st rows, in that order.

In addition, once a mattrib statement has been used to define row and column headings, those headings can also be used in the subscript specification instead of the numbers.

Video:  Incorporating Headings into a Matrix

You could even make the row and column headings part of a new matrix.  However, if they are character vectors (as usual), the matrix must be a character matrix.  Look at the following:

    x2=("Row"||head)//(rowh||x);

Visualize the results or try an example.  The Char function may be used to convert a numeric matrix to character form for this purpose.

Video: Using "do" loops and random sampling

We bring in the usedcars data set for the next example.  If work.usedcars doesn't exist, run a program to load the usedcars3.txt file into a sas data set.  Usedcars has both character and numeric data.

    use usedcars;
    read all into UC;
    print UC;

This will bring in only the numeric variables (there are three). 

    read all var _num_ into UC;

is equivalent, because it specifies numeric variables, which is the default.  You can include this to make your program more readable.

To create a character matrix for the character variables, write:

    read all var _char_ into UC2;

Now we'll learn something about do loops.  We begin with the J function to create a column of 1's and the following do loop:

    Y=J(34,1);
    do i=1 to 34;
        Y[i,1]=uniform(0);
    end;

Note that the editor turns the "do" red but it is not an error, it is some problem in the editor.  Now do the following:

    Yr=rank(Y);
    UCsample=UC[Yr[1:10,1],];

Look carefully at what is happening here.  Yr contains the rank values of the random numbers in the Y vector.  I want to choose 10 randomly sampled rows from UC.  I can get the indices from the first 10 entries of Yr.  Remember they don't have to be in order.  So that is what "Yr[1:10,1]" gives.  It is in essence a list of 10 index numbers for rows I want to select.  Now, I use that list or vector to subscript the UC matrix, and take all the rows, assigning the result to UCsample.  Notice that Yr is actually a column vector, but the subscript notation accepts either row or column vectors for arguments.

Now here is an alternative way to get the random numbers.

    Y=uniform(0);
    do i=1 to 33;
        Y=Y/uniform(0);
    end;

This works by starting with a random scalar, then vertically concatenating 33 more random numbers to it, building up the Y vector that way.

 

<<Previous|Contents|Next>>

Exercises:

Copyright reserved by Dr.  Dwight Galster, 2006.  Please request permission for reprints (other than for personal use) from dwight.galster@sdstate.edu  .  "SAS" is a registered trade name of SAS Institute, Cary North Carolina.  All other trade names mentioned are the property of their respective owners.  This document is a work in progress and comments are welcome.  Please send an email if you find it useful or if your site links to it.