Monday, March 27, 2017

Hack 4.17 Sorting Variables Within an Observation


SAS Programming Professionals,

Did you know that you can sort the values of variables within an observation into ascending order? 

The SORTN CALL routine allows you to do just that.  Here is an example:

options nodate nonumber;

/* Create the Test data set*/
proc sort in=sashelp.mdv out=work.mdv(keep=origcity shipdate);
      by origcity;
run;

proc transpose data=mdv out=mdvtrans prefix=date;
      by origcity;
      var shipdate;
run;

/* Print the obs with unsorted dates */
proc print data=mdvtrans;
title1 "Dates Not Sorted";
run;

/* Use the SORTN call routine to sort dates */
data sorted_MDV_dates;
set  mdvtrans;

array sortdate{28} date1 - date28;

call sortn (of sortdate(*));

run;

/* Print the obs with sorted dates */
proc print data=sorted_mdv_dates;
title1 "Dates Sorted";
run;

The first part of the program sets up the data set that we will use in the DATA Step that is highlighted in red.  The first steps create a SAS data set with 28 date variable in it.  That data set is output in the first PROC PRINT.  In the DATA Step we use the SORTN to sort the dates (DATE1 – DATE28) into ascending order.  The second PROC PRINT reveals that DATE1 – DATE28 have been sorted into ascending date sequence.  One statement; it’s that simple!

CALL SORTN can be useful in cases where you need to order a series of event dates in an observation for a particular subject id.  Note that its twin function, CALL SORTC, can be used to sort character variables into ascending order within an observation.

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

Monday, March 13, 2017

Hack 4.16 Simplifying IF/THEN/ELSE Statements Using the IFC and IFN Functions


SAS Programming Professionals,

Did you know that the IFC and IFN functions allow you to have 1-line IF/THEN/ELSE-like statements? 

The IFC and IFN functions can be handy when you have a single THEN condition and a single ELSE condition and you would like to have an action taken if the test condition results in missing values.

The IFC function returns a character value based on whether or not an expression is true, false, or missing.  The basic form of the function is:

               IFC(logical-expression, value-returned-when-true, value-returned-when-false <,value-returned-when-missing>)

Here is an example:

data class_gender;
set  sashelp.class;

length gender $7;
label gender = "Gender";

gender = ifc(sex="M", "Male  ", "Female", "Unknown");

run;

In the example, we test variable SEX for the value “M”.  If SEX = “M”, the variable GENDER is set to “Male”.  If not, it is set to “Female”.  If SEX contains a missing value, it is set to “Unknown”.

The IFN function returns a numeric value based on whether or not an expression is true, false, or missing.  The basic form of the function is:

               IFN(logical-expression, value-returned-when-true, value-returned-when-false <,value-returned-when-missing>)

Here is an example:

data electric_surcharge;
set  sashelp.electric;

label surcharge = "Surcharge (B)";

surcharge = ifn(year=2005, revenue *.001, revenue * .000025, 0);

run;

In the example, we test the variable YEAR for the numeric value of 2005.  If YEAR = 2005, the variable SURCHARGE is set to REVENUE * .001.  If YEAR does not equal 2005, SURCHARGE is computed to be REVENUE * .000025.  If YEAR contains missing values, SURCHARGE is set to Zero.

You can probably think of dozens of uses for the IFC and IFN functions.  I like to think of these two functions as “IF/THEN/ELSE in a can”.

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

Monday, March 6, 2017

Hack 4.15 Setting Multiple Variables to Missing in a Single Statement


SAS Programming Professionals,

Did you know that you can easily set one or more SAS variables to missing values in a single statement?

The CALL Missing SAS call routine allows you to set character and numeric variables to missing values.  Call Missing sets numeric variables to numeric missing (.), and character variables to character missing (blank).  The basic format is:

CALL MISSING(varname1<, varname2, ....>);

Here is an example:

data vetted_class;
set  sashelp.class;

if age < 14 then call missing(weight, height, sex);

run;

In the example, we have specific criteria for setting variables to missing—when the subject is younger than 14 years old.  When that is the case, we set the values of weight, height, and sex to missing.  Note that weight and height are numeric variables, while sex is a character variable.  It makes no difference at all to CALL MISSING; it sets them all to missing!

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

Monday, February 27, 2017

Hack 4.14 Running System Commands from a SAS Program


SAS Programming Professionals,

Did you know that you can execute Windows operating system commands from within a SAS Data step?

You can do this by using the CALL SYSTEM call routine.  Unlike its blunt cousin the X statement, CALL SYSTEM can be executed conditionally within a DATA step.  This makes it much more flexible and useful.  Here is an example:

/* Rename directory on Mondays*/
data _null_;

if weekday(today()) = 2 then do;

      call system("rename c:\temp\mikedir mikedir_old");
      call system("md c:\temp\mikedir");

end;

run;

ods rtf file="c:\temp\mikedir\classlist.rtf";

proc print noobs data=sashelp.class;
title1 "List of Subjects in Weight/Height Study";
run;

ods rtf close;

In the example, the DATA _NULL_ step houses the CALL SYSTEM call routines.  They are only executed when the current day of the week is Monday.  When they are executed, the first CALL SYSTEM sends a command to Windows to rename the MIKEDIR directory to MIKEDIR_OLD.  The second CALL SYSTEM sends a command to Windows to create a new directory named MIKEDIR.  The subsequent PROC PRINT creates an RTF file in the new MIKEDIR directory.

Obviously, the Windows system commands must be syntactically correct for the CALL SYSTEM call routine to work properly.  Here is one of the many online sources for looking up Windows system commands:


The previously mentioned X command executes unconditionally.  That means that if we were to replace both CALL SYSTEM’s with X commands in the program above, they would executed no matter what day of the week it is.  They just barge right in.  That is why I prefer the CALL SYSTEM call routine for shelling out operating system commands from within my SAS programs.

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

Monday, February 20, 2017

Hack 4.13 Reversing Character Strings with the REVERSE Function


SAS Programming Professionals,

Did you know that: the REVERSE function can be very useful and also a lot of fun to use?

The REVERSE function can be used to return a character string in reverse order.  For instance, consider the following example:

data testit;

x = "SAS";
Y = reverse(x);

put "x= " x;
put "y= " y;

run;

As you can see from the log:

1    data testit;
2
3    x = "SAS";
4    Y = reverse(x);
5
6    put "x= " x;
7    put "y= " y;
8
9    run;

x= SAS
y= SAS

… Y is the exact reverse of X!

A more practical application of this can be found when you need to pluck out the last node in something, such as a full-path file name.  Here is an example from a SAS program that I use to get the name of previously executed SAS programs from the LOGFILE variable in LOGPARSE SAS data sets:

/**********************************/
/* Determine the SAS program name */
/**********************************/
logtemp = reverse(strip(logfile));

r = index(logtemp,"\");

substr(logtemp,r,length(logtemp) - r + 1) = " ";

SASprogram = tranwrd(strip(reverse(logtemp)),"log","sas");


In the example, I know that logfile has the full path name of a SAS log, such as:  "c:\production\fy02\bigprog.log".  If the name of the log is "bigprog.log", then the program that was executed must be "bigprog.sas".  That is what I want to capture in variable SASprogram.

So, the snippet of code first reverses LOGFILE, then looks for the first "/", which was originally the last "/", but is now the first one since the full path has been reversed.  It turns the rest of the contents of LOGTEMP into blanks.  Then, it sets SASprogram equal to the strip-ed, reverse (again) of LOGTEMP while translating "log" into "sas".  Neat, clean, and effective!

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

Monday, February 13, 2017

Hack 4.12 Renaming Files and Directories from A DATA Step


SAS Programming Professionals,

Did you know that you can rename SAS data sets, SAS catalog entries, as well as OS directories and files in directory-based operating systems from inside of a SAS DATA step? 

The RENAME function lets you do exactly that!  It is great for SAS purists who do not want to use the X command or the CALL SYSTEM routine to “shell out” to the operating system to rename files and/or directories.  And, if you have PROC DATASET phobia, you can use the RENAME function to rename SAS files.

Here is a simple example:

data _null_;

rc = rename('c:\temp\test.pdf', 'c:\temp\old.pdf', 'file');

put "*****************";

put "the return code is: " rc;

put "*****************";

run;

The example above renames “test.pdf” to “old.pdf” in a DATA step.  The nice thing about the RENAME function that differentiates it from the X command or the CALL SYSTEM routine is that you get a return code that you can query to make sure that your rename worked.

Intrigued?  Well, then read more about it in the SAS online documentation on support.sas.com

Best of luck in all your SAS endeavors!


---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

Monday, February 6, 2017

Hack 4.11 Removing Leading and Trailing Blanks with the STRIP Function


SAS Programming Professionals,

Did you know that there is a function that will effortlessly strip off leading and trailing blanks from a character variable?

The aptly named STRIP function allows you to do just that and is a great replacement for the old TRIM(LEFT(character-variable)) construct many programmers used to use.

Here is an example of using the STRIP function:

data cars;
set  sashelp.cars;

car_statement = "The " || strip(make) ||
                   " " || strip(model)||
              " is a " || strip(type) ||
              ".";

run;

proc print noobs data=cars;                    
      var car_statement;
run;

Which produces, in part:

The Acura MDX is a SUV.
The Acura RSX Type S 2dr is a Sedan.
The Acura TSX 4dr is a Sedan.
The Acura TL 4dr is a Sedan.
The Acura 3.5 RL 4dr is a Sedan.
The Acura 3.5 RL w/Navigation 4dr is a Sedan.
The Acura NSX coupe 2dr manual S is a Sports.
The Audi A4 1.8T 4dr is a Sedan.
The Audi A41.8T convertible 2dr is a Sedan.

In the example, we are constructing variable CAR_STATEMENT by concatenating string constants and concatenating the variables MAKE (13 characters in length), MODEL (40 characters in length) and TYPE (8 characters length).  Without the STRIP function, there would be big gaps in CAR_STATEMENT because many of the MAKEs, MODELs, and TYPEs are not 13, 40, and 8 characters long, respectively.

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers