Monday, January 30, 2017

Hack 4.10 Removing Leading and Trailing Blanks with the CATX Function


SAS Programming Professionals,

Do you know that you can quickly and easily remove leading and trailing blanks when concatenating character variables together?

If you were thinking “Ah yes, the CATS function!” then give yourself a prize!  However, this tip is really about the CATX function.

The CATX function removes leading and trailing blanks and inserts a separator between the variable values being concatenated.  (The CATS function only removes leading and trailing blanks).   This function is handy for building meaningful character variables from two or more variables—like building a full name from first name, middle name, and last name.

Here is an example:

data names;

firstname  = "       Francis";
middlename = "Scott         ";
lastname   = "   Key        ";

fullname = catx(" ", firstname, middlename, lastname);

run;

proc print noobs data=names;
run;

You can see that FIRSTNAME has leading blanks, MIDDLENAME has trailing blanks, and LASTNAME has both leading and trailing blanks.  The CATX function specifies that the separator should be a blank.  So, the result of the PROC PRINT is:

     firstname    middlename    lastname         fullname

     Francis       Scott         Key       Francis Scott Key

If you like the CATX function, check out its brothers, the CAT, CATT, and CATS functions!

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, January 23, 2017

Hack 4.9 Removing Blanks From Macro Variable Text


SAS Programming Professionals,

Did you know that the CALL SYMPUTX routine can save you keystrokes and lead to leaner, cleaner SAS programs? 

In the past, when we needed to load a SAS macro variable with a character string that might contain blanks, we would do the following:

call symput('MACROVAR',trim(left(charvar)));

This can be reduced by 12 keystrokes by using CALL SYMPUTX in the following manner:

call symputX('MACROVAR',charvar);

Think how happy your tired fingers will feel with a savings like that!

My older programs are littered with "trim(left)" CALL SYMPUT's and I would bet that yours are too.  I plan to replace mine as time and circumstance allows when I open my programs to address other issues.  How about you?

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, January 16, 2017

Hack 4.8 Putting Your SAS Programs to Sleep


SAS Programming Professionals,

Did you know that you can actually put your SAS programs to sleep?

The SLEEP function suspends execution of your SAS program (puts it to sleep) for the duration of time that you specify in the function.  This function comes in handy when you need to programmatically pause your SAS programs for various reasons.  One such reason is to wait for an asynchronous system event to occur before your SAS program continues.  For example, if you use an X statement to invoke an operating system command, you could put your program to SLEEP for the duration that it takes for that command to complete.

The general form of the SLEEP function is:  Q = SLEEP(N); …where:

·        Q = any SAS variable

·        N = the number of seconds to wait.  The maximum is 46 days!  See the documentation if you want to specify other intervals, such as micro-seconds.

Here is an example of the SLEEP function in action:

options noxwait noxsync xmin;

x '"C:\Program Files\Microsoft Office\Office11\excel.exe"';

data _null_;
  x=sleep(10);
run;

filename comma1 dde 'excel|system';

data _null_;
 file comma1;
 put '[open("D:\MCR\t_excel.xls")]';
run;

data _null_;
  x=sleep(10);
run;

…Other SAS code to populate the Excel spreadsheet…

The overall thrust of this example is to use DDE to open and populate an Excel spreadsheet from a SAS program.  First, we use the SAS X statement to send a command to Windows to open Excel.  Secondly, we put our program to sleep for 10 seconds in a DATA _NULL_ step to give Windows enough time to complete opening Excel.  Next, the FILENAME statement sets up our DDE “environment” and the second DATA _NULL_ creates our spreadsheet in the opened Excel program.  Then, we put our SAS program to sleep a second time to give Excel enough time to create the t_excel.xls spreadsheet in the opened Excel program in the aforementioned DATA _NULL_ step.

If you are tired of having your programs fail due to SAS-generated asynchronous tasks not completing on time, consider using the SLEEP function!

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, January 9, 2017

Hack 4.7 Increasing the Accuracy of Age Calculations with the YRDIF Function


SAS Programming Professionals,

Did you know that the YRDIF function can increase the accuracy of your age calculations?

The YRDIF function returns the number of years between two dates.  So, it can be useful in cases where you are looking for age, years in a study, duration of dosage, etc.  The format of the YRDIF function is:

               YRDIF(start-date, end-date, basis)

…where basis describes how SAS should calculate the difference between dates.  The most common basis is “ACT/ACT”, which specifies that SAS is to calculate YRDIF as the number of days that fall in 365-day years divided by 365 plus the number of days that fall in 366-day years divided by 366.  Here is an example:

data mybirthday;

     my_age_in_years = yrdif("27APR1975"d,today(),"ACT/ACT");

     label my_age_in_years = "My Current Age";

run;

proc print noobs label data=mybirthday;
run;

In the example, we determine the difference between my birth date and today’s date.  I entered a date literal for start-date and the TODAY() function for end- date, but I could have easily used two variables containing date values, instead.  The SAS listing looks like this:

My Current Age

   39.1534

Of course the year calculation is only as accurate as the information you feed into the YRDIF function.  If you are curious about other possible values for basis, look up the YRDIF function 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

Tuesday, January 3, 2017

Hack 4.6 Determining The Dates of Major Holidays with the HOLIDAY Function


SAS Programming Professionals,

Did you know that the HOLIDAY function allows you to determine the date of major US and Canadian holidays? 

You simply supply the HOLIDAY function with the holiday keyword and the year, and it will return the SAS date value.  The syntax looks like this:

               HOLIDAY('holiday',year)

Here is an example that computes three holidays for the next ten years:

data future_holidays;

label year = "Year"
      mothersday= "Mothers Day"
      presidentsday = "Presidents Day"
      victoriaday = "Victoria Day"
      ;
format mothersday presidentsday victoriaday worddate.;

do year = 2015 to 2025 by 1;

mothersday = holiday("MOTHERS",year);
presidentsday=holiday("USPRESIDENTS",year);
victoriaday=holiday("VICTORIA",year);
output;
end;

run;

proc print noobs label;
run;

There are currently twenty-two holiday keywords for the HOLIDAY function.  You can read more about the HOLIDAY function, including all of the available holiday keywords, in the SAS online documentation on the support.sas.com web site.
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