Friday, May 6, 2016

Hack 3.5 Creating a PROC EXPORT or PROC IMPORT Template Program

SAS Programming Professionals,

Did you know that you don't have to memorize the exact syntax and all of the options of PROC EXPORT or PROC IMPORT, or go fumbling for the documentation every time you need to use them? 

Instead, create a PROC EXPORT template and a PROC IMPORT template for yourself using the wizards in SAS Display Manager.  Then, copy either template into your programs and modify the code to suit the particular needs of the export or import situation at hand.

In SAS Display Manager, select: <File>, then <Export Data>.  Choose the SASHELP.CLASS SAS data set just to have something to work with.  Then, go through all of the subsequent windows, choosing each and every one of the available options.  When you get to the window that states:

"The EXPORT wizard can create a file containing PROC EXPORT commands that can be used in SAS programs to export this data again.  If you want these statements to be generated, enter the filename where they should be saved."

…choose a directory and then a meaningful program name, such as "Export to Excel Template.sas".  When you click <Finish>, you will have a SAS program that looks something like this:

PROC EXPORT DATA= SASHELP.CLASS
     OUTFILE= "C:\Documents and Settings\RAITHEL_M\SAS Class.XLS"
     DBMS=EXCEL LABEL REPLACE;
     RANGE="SAS Class";
     NEWFILE=YES;
RUN;

You can reverse this process with the Import Data wizard to create a PROC IMPORT template.  Now how hard is that?  A little pre-planning now, may save you a lot of time later!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

Monday, May 2, 2016

Hack 3.4 Creating a Detailed Directory Listing of Files in A SAS Data Library

SAS Programming Professionals,

Did you know that you can get a detailed directory listing of the files in a SAS data library without having to print all of the detail information for each and every SAS data set in it? 

The listing is called "Library Members" and contains the following information: 

·        Name - Name of the SAS data set, Index, Catalog, etc.

·        Member Type - Data, Index, Catalog, etc.

·        Level - If the SAS data library is composed of concatenated directories, this is the concatenation level at which this file can be found

·        Obs, Entries, or Indexes - The number of observations for a SAS data set, entries for a SAS catalog, and indexes for an index file

·        Vars - Number of variables for a SAS data set

·        Label - The file’s label

·        File Size - Size of the file

·        Last Modified - Date-time stamp of when the file was last modified

This is great information to have if you need to conduct a quick visual inspection of a SAS data library you are not familiar with.

Here is code to produce a Library Members listing:

proc contents data=sashelp._all_ memtype=data details nods;
run;

The DETAILS option enables the more detailed "Library Members" listing and the NODS option turns off that pesky listing of individual data set details.

Because of its size, it is not really practical to print the output to the PROC CONTENTS above in this blog.  Copy it into a SAS program and execute it to see the wealth of information that is available about the members of a SAS data library.

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

Monday, April 25, 2016

I Saw Elvis at SAS Global Forum 2016!

SAS Programming Professionals,

Urban legends and conspiracy theories are generally not my thing.  I have never been abducted by a UFO; I have never had an encounter with Big Foot, Sasquatch or Nessie; I believe the Egyptians were actually the ones responsible for building the pyramids; and I do not have any particular reason to doubt the Warren Commission report.  However, I did see Elvis at the Happy Hour: Tap and Brew in the Quad event at SAS Global Forum 2016!


And, it does make sense in a Las Vegas kind of way:  The King of Rock 'n Roll meets the King of Analytics.  Right?

Right!


I was a bit disappointed that Elvis was not one of the 200+ people who packed my Monday morning presentation, Paper 3440-2016: PROC DATASETS; The Swiss Army Knife of SAS Procedures.  

If you were not able to attend, here is a link to a recording of that very session:


I was not the only person to have a recorded SAS Global Forum presentation; here is a link to the other 90 recorded sessions:  http://sasgf16.v.sas.com/category/videos/breakout-sessions?page=2


If you prefer the printed word to the spoken word, then you can’t go wrong accessing the proceedings at this link:  http://support.sas.com/resources/papers/proceedings16/index.html

If you are not actively reading SAS conference proceedings to boost your SAS programming knowledge, well then: You ain't nothin' but a hound dog, cryin' all the time:-)!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel) 
Twitter: @MichaelRaithel 

Amazon Author Page:

Friday, April 15, 2016

Hack 3.3 Creating a Detailed Contents Listing of A SAS Catalog


SAS Programming Professionals,

Did you know that you can create a detailed listing of the contents of a SAS catalog?

To do so, simply specify the CONTENTS statement in the CATALOG procedure.  This can come in handy when you receive a catalog from out-of-house that is ostensibly a format catalog associated with variables in an attendant SAS data set.  

You can determine what, exactly, is in the catalog by running PROC CATALOG thusly:

      libname newproj "H:\newproject\inputlib";

      proc catalog catalog=newproj.myfmts;
           contents stats;
      quit;

The CONTENTS statement instructs SAS to display the objects stored in the catalog.  The STATS option directs SAS to provide all of the statistics available for the entries in the format catalog.  Without the STATS option, a more abbreviated table is created.
Running the CATALOG procedure shows that the catalog I received contains only two formats:

Contents of Catalog NEWPROJ.MYFMTS
#
Name
Type
Create Date
Modified Date
Description
Page
Size
Block
Size
Num of
Blocks
Last
Block
Bytes
Last
Block
Size
Pages
1
AGEGROUP
FORMAT
22Jun14:12:51:45
22Jun14:12:51:45
 
4096
4096
1
245
510
1
2
GENDER
FORMATC
22Jun14:12:51:45
22Jun14:12:51:45
 
4096
4096
1
219
255
1

  …and I was expecting one format for each of the 427 variables in the SAS data set they sent.  So, I will have to contact the sender with news of my own QC of the format catalog they sent!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers


I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

Monday, April 11, 2016

Hack 3.2 Copying A SAS Data Set to the Same SAS Library

SAS Programming Professionals,

Did you know that you can make an exact copy of a SAS data set in the same library as the original using a SAS procedure?

If you are thinking that you cannot use PROC COPY to do this, then you are absolutely right!  PROC COPY does not have a facility for specifying a new name for the data set that is being copied.

The SAS procedure to use is… (drum roll, please): PROC APPEND!  The technique to use is to append the data set you want to have “copied” to a non-existent data set with the new name that you want for the copy.  For example, say that we have a SAS data set named HEART and want to create a copy of that data set named HEART_BACKUP in the same library.  We would code the following:


libname ctemp "c:\temp";

proc append base=ctemp.heart_backup
            data=ctemp.heart;
run;

That would result in the following being written to the SAS log:

2    proc append base=ctemp.heart_backup
3                data=ctemp.heart;
4    run;

NOTE: Appending CTEMP.HEART to CTEMP.HEART_BACKUP.
NOTE: BASE data set does not exist. DATA file is being copied to BASE file.
INFO: Engine's block-read method is in use.
INFO: Engine's block-write  method is in use.
NOTE: There were 5209 observations read from the data set CTEMP.HEART.
NOTE: The data set CTEMP.HEART_BACKUP has 5209 observations and 17 variables.

I highlighted the second NOTE which states that the BASE data set—HEART_BACKUP in this case—doesn’t exist.  That NOTE is confirmation that you did not overwrite an existing SAS data set, but created a copy from the original.  Now you are one of the very few SAS professionals who know the secret to using a SAS procedure to copy a SAS data set to same data library as the original!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE

(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers


I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

Friday, April 8, 2016

Hack 3.1 Controlling the Levels Displayed in One-Way PROC FREQ Tables


SAS Programming Professionals,

Did you know that you can control the number of levels displayed for one-way tables in PROC FREQ?

You can do this by using both the ORDER= option on the PROC FREQ statement and the MAXLEVELS= option on the TABLES statement.  Here is a simple example that you can copy and run:
/* Traditional PROC FREQ with all levels produced */
proc freq data=sashelp.cars order=freq;
      tables make;
       run;
 
/* PROC FREQ with only top 5 levels produced */
proc freq data=sashelp.cars order=freq;
      tables make / maxlevels=5;
run;

In the second PROC FREQ, the ORDER=FREQ option specifies for SAS to list the output in descending order.  The MAXLEVELS=5 option specifies for SAS to only print the first five levels.

The second PROC FREQ produces the following report:
                                        The FREQ Procedure
                                                           Cumulative    Cumulative
                Make             Frequency     Percent     Frequency      Percent
                ------------------------------------------------------------------
                Toyota                 28        6.54            28         6.54
                Chevrolet              27        6.31            55        12.85
                Mercedes-Benz          26        6.07            81        18.93
                Ford                   23        5.37           104        24.30
                BMW                    20        4.67           124        28.97

                                The first 5 levels are displayed.

Notice the nice note stating that only the first five levels are displayed.  This can come in handy for times where you want an abbreviated report of only the top N frequencies.

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.


Monday, April 4, 2016

Hack 2.14 Using the DATALINES and INFILE Statements to Test Input From External Files


SAS Programming Professionals,

Did you know that you can use the INFILE statement with the DATALINES statement?

This is handy in situations where you would like to test the results of inputting an external file by including some sample input data beneath the DATALINES statement.  Once you have vetted your INPUT statements, you would:

  • Code a FILENAME statement before the data step that points to the data you intend to process, 
  • Change the fileref that is coded in your INFILE statement from “datalines” to what is specified on the FILENAME statement,
  • Remove the DATALINES statement and the sample input data from the DATA step.

 Observe this simple example: 

data didyouknow;
infile datalines missover;
input firstname $ age;
datalines;
Michael 35
Minerva
Mark 25
Miranda
;;;;
run;

proc print;
run;

In the example, the INFILE statement specifies DATALINES as the fileref SAS is to use for inputting data to the DATA step.

The output from executing this example looks like this: 

Obs
firstname
age
1
Michael
35
2
Minerva
.
3
Mark
25
4
Miranda
.

 Note that the MISSOVER option is absolutely necessary in this example.  And, it can only be specified on an INFILE statement.  The MISSOVER option is necessary because the sample data does not always contain a value for AGE.  Without the INFILE statement and the MISSOVER option, SAS would not know it needs to go to a new line when the INPUT statement reached past the end of the current line.  So, when it got to the second line of input, it would attempt to go to the third line and read “Mark” into the AGE variable, resulting in a pesky note in the log: 

NOTE: Invalid data for age in line 24 1-4.

RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+---

24         Mark 25

firstname=Minerva age=. _ERROR_=1 _N_=2

NOTE: LOST CARD.

 This means that you would not have been able to adequately test your program with a DATALINES statement without the INPUT statement.

If you prefer to use the CARDS statement to read inline data fear not; it can also be used the same way!  Simply specify CARDS as the fileref in the INFILE statement.  It is always nice to have choices!

Best of luck in all of your SAS endeavors!
----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel) 

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers


I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.