Lesson 34:  SAS Data Sets and Matrices


Video:  Saving Matrices in SAS Data Sets

Let's say we have the dataset called "usedcars" in the work or user library.  This time, we are going to select the first 10 observations into two matrices.  We can write:

    proc iml;
    reset printall;
    use usedcars;
    read point (1:10) into UC;
    read point (1:10) var _char_ into UCC;

Read the documentation in the IML section about working with SAS data sets for details.

Now we want to go backwards, sending the contents of matrices back into a SAS data set.

    create ucs from uc;
    use ucs;
    list all;

The create statement only creates an empty data set.  The append statement adds the data from the matrix to the dataset.

To specify the variable names for the data set, use

    create ucs from uc[colname={"year" "miles" "price"}];

Video:  Creating a data set with a var clause

This method with the from clause will only work with one matrix and therefore one type of data at a time.  The other method is to use create with a var clause.  In order to do this, each variable should be in a vector.

    create uccs var{year miles price make model color stock};
    use uccs;
    list all;

To delete a data set so you can replace it, use

    call delete("ucs");

To create data sets that have both numeric and character variables, name vectors that have the same name as the variables in your data set and and put them in a create statement this way:

    create mix var{X1 X2 Y};
    show contents;
    list all;

The var list should consist of vectors.  If you put in a matrix it will list all the elements in a column.

Another useful command allows sorting a data set from within IML.

    sort data=uccs by miles;

We can also sort a matrix by any column.

    call sort(uc, by year);



Read the material in SAS Documentation on modules.

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.