Tag Archives: Code Tips

On ErrorAbend

One issue I always had with the SAS system as a developer was when I had a job that ran in batch that had an error. The SAS System would set the number of observations to zero and go into syntax checking mode for the remainder of the program.

This had some virtues but more often than not, the error was thrown because I had misspelled a variable name in a MEANS statement or FREQ statement that was used for checking my output. This would cause SAS to go into the syntax checking mode and all the rest of my program would not execute even though it was proper.

WPS, when running in batch doesn’t do this but if you want the same effect for your batch jobs, it’s easy enough to implement. Consider the following macro – called %ErrorAbend. %ErrorAbend simply checks that the program is not running in the FOREground and checks the value of the &syserr variable after every PROC or data step and if it returns a value of 3, then issues a note and sets the number of observations to zero.

%macro onerrorabend;
  %if %eval(&syserr eq 3) and &sysenv NE FORE %then %do;
     options obs=0;
     %put NOTE: WPS has been set with OPTION OBS=0 and will continue to check statements.
  %end;
%mend;

Below is a sample program that when run in batch, puts the system into syntax checking mode and basically stops the execution of any downstream statements.

data a b;
do ii=1 to 2000;
  x=ranuni(0)* 10;
  y=Round(ranuni(0),.01)* 100;
  z=round(ranuni(0),.01)* 10000;
  

  a=ranuni(0)* 10;
  b=Round(ranuni(0),.01)* 100;
  c=round(ranuni(0),.01)* 10000;
  
  e=ranuni(0)* 10;
  f=Round(ranuni(0),.01)* 100;
  g=round(ranuni(0),.01)* 10000;
  
  i=ranuni(0)* 10;
  j=Round(ranuni(0),.01)* 100;
  k=round(ranuni(0),.01)* 10000;

  output;
end;
run;

proc freq data=a;
tables ik;
run;

%onerrorabend;


proc means data=b;
run;

About the author: Phil Rack is President of MineQuest Business Analytics, LLC located in Grand Rapids, Michigan. Phil has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and is a authorized reseller of WPS in North America.

Integrating WPS and Skype

One of the things I like to do in programming is integrating other tools with WPS. I don’t know why I find that so fun to do, but I just find it rewarding for some reason. Every once in a while, I stumble across a problem to be solved and it just seems like it should be something that is easy, simply because it’s so useful. That is rarely the case as we all know.

I always wanted to have some way for an interactive WPS job or a WPS batch job to notify me of an error when it reaches completion or to notify me that it has reached a certain point in the job process. One of the main requirements is that the notification be sent by text message so that I could be notified if I wasn’t sitting directly in front of the monitor of the machine that the code was submitted on.

We have been able to send text messages for years (see “Sending SMS Text via WPS”) but that was kind of clumsy because as far as I could tell, it would only send the message to my cell phone. I want more! Give me more!

Skype is the logical choice for me because I sit in front of a PC most of the day that has Skype running. I also have Skype installed on Skype is the logical choice for me because I sit in front of a PC most of the day that has Skype running. I also have Skype installed on mobile Apple and Android devices so I can receive messages through those devices pretty easily.

All I needed to do is install Skype on a Windows Server. The Server I installed it on is running Windows Essentials 2012 R2. Getting Skype on that platform can be a chore but it can be done. I ended up installing the Windows 8 version of Skype on the server and after getting it to run properly (i.e. sending and receiving messages) I installed the classic Skype for Desktop on the machine. If you are running a previous version of Windows Server… well good luck on getting it to install.

Usage

%WPS2Skype(SkypeID=minequest_llc,

                         category=-U,

                         SkypeMsgTxt = “Your job reached the halfway point you awesome guy!”);

 

Where

SkypeID is the ID of the recipient of the message

Category = -U or –CC

SkypeMsgTxt =”Your message text here.”

 

If you are a WPS programmer or are familiar with the macro language, then the above should be self-explanatory with the exception of Category. Category can take one of two options, either –U or –CC. If we want to send the message to an individual user than we use –U. If we want to send the message to a list of contacts then we use –CC. A contact list is somewhat similar to a group and you should read up on what a contact list is and how it can be created in the Skype documentation.

Running the following code:

%WPS2Skype(SkypeID=xxxxxxxxxx,

                         category=-U,

                         SkypeMsgTxt = “Your job reached the halfway point you awesome guy!”);

 

We see in the WPS log the output when the message was sent:

 

2         %WPS2Skype(SkypeID=xxxxxxxxxx,

3         category=-U,

4         SkypeMsgTxt=”Your job reached the halfway point you awesome guy!”);

 

NOTE: WPS2Skype returned execution to WPS.

 

And what I see in the Skype message window on my workstation is:

 

 

 

clip_image001

 

 

Of course, this will show up on all the devices that I have logged in to Skype using my Skype ID.

 

I also extended the Skype interface into the RunWPS.CMD file. The RunWPS.CMD is a Windows command file that allows you to run WPS as a batch job. With the integration of Skype with RunWPS.CMD, I catch all return codes from WPS and send out a Skype message if the return code is greater than zero. This is easily modified but I’m not personally interested in getting messages when jobs complete properly. I’m only interested when jobs terminate abnormally.

 

The WPS2SKYPE utility will be available in our next release. There are a few more utilities that I want to add to the package and I still have documentation that needs to either be written or cleaned up.

 

About the author: Phil Rack is President of MineQuest Business Analytics, LLC located in Grand Rapids, Michigan. Phil has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and is a authorized reseller of WPS in North America.

WPS Tip – RegEx Testing

One of the nicer things about WPS is the extendibility of the Eclipse Workbench. You can extend the Workbench by use of plugins. There are lots of plugins available that you can use but you need to understand one restriction that is currently in-place. That restriction is that you can only install a plugin to the WPS environment by manually copying the file into the Eclipse directory and NOT by using the Software Updates method.

There are a couple of plugins that I have in my environment but the one I want to show you is a regular expressions tester. Since WPS supports the language of SAS, and that includes Regular Expressions using the PRX functions, having a RegEx Tester is quite convenient.

You can download and install the RegEx Tester plugin from Stephan Brosinski web site at: http://www.brosinski.com/regex/

Choose “Download the most recent version as a zip file” and simply follow the installation directions he provides at the bottom of the page. Once you have it installed, the RegEx Tester will appear on the right hand side of the Workbench. Below is a screen shot of some demo text as it appears in the RegEx Tester.

image

There are a number of web sites that offer tutorials on Regular Expressions. Two websites offering tutorials on Regular Expressions can be found at http://www.aivosto.com/vbtips/regex.html and also at  http://www.regular-expressions.info/index.html

About the author: Phil Rack is President of MineQuest, LLC. and has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and a reseller of WPS in North America.

WPS Tips – Start a New Session

As most of you who have been using WPS via the Eclipse Workbench already now, you can submit a WPS program and instantly start modifying code, view the log window or listing window without impact to your existing session. You can even start a new program editor all the while your WPS program is running in the background. Try that with the SAS DMS!

One thing you can’t do is submit a second WPS program while you are running a program in an existing session. But here’s a neat trick that allows you to run a second program simultaneously and take advantage of all that horsepower of your PC. By simply starting a new WPS session you can run another WPS program while your first one in session #1 is executing.

To do this, simply go to the main menu and click on Window and then select New Window. A new WPS session will start-up with all your existing programs in the Project Explorer Window

 

image

About the author: Phil Rack is President of MineQuest, LLC. and has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and a reseller of WPS in North America.

WPS Tip – Graphics Catalogs

Playing around with WPS Graphs, I sort of stumbled a bit trying to save the generated graphs to a catalog. In retrospect, it was pretty straight forward. Here I’m going to present some code so you can store your plots and graphs in a permanent catalog too.

By the way, the first part of this code is very similar to what I published the other day. About the only difference is that I’m using a device=gif statement instead of device=jpeg. Although I’m not 100 percent sure, I believe that jpeg and gif are the only two options for the device statement at this time.

Here’s the code that creates the three plots and stores them in a graphics catalog.

   1:  

   2: Proc format;

   3: value $gender

   4: 'F' = 'Female'

   5: 'M' = 'Male'

   6: ' ' = 'N/A';

   7: run;

   8:  

   9:  

  10: data a;

  11: input Name $1-5 @7 Gender $1. testscore 3.;

  12: cards;

  13: Alice F 100

  14: Bob   M 101

  15: Jim   M 102

  16: Chris F 104

  17: Donna F 106

  18: Ed    M 110

  19: Frank M 115

  20: Gwen  F 116

  21: Hank  M 120

  22: Nancy F 118

  23: Paula F 122

  24: Susan F 124

  25: Terry F 125

  26: ;

  27: run;

  28:  

  29:  

  30: libname graphcat 'c:\demolib\graphcat\';

  31:  

  32: *--> delete existing entries in the catalog;

  33: proc greplay igout=graphcat.GenderGraphs nofs;

  34: delete _all_;

  35: run;

  36:  

  37:  

  38:  

  39: footnote;

  40: goptions reset=global gunit=pct border 

  41: cback=white colors=(blue red yellow green) 

  42: ctext=black ftitle=Vera ftext=Vera 

  43: htitle=4 htext=3 device=gif;

  44: pattern1 color = red value=solid;

  45: pattern2 color = blue value=solid;

  46:  

  47:  

  48:  

  49:  

  50: Proc Gchart data=a gout=graphcat.GenderGraphs;

  51: hbar Gender;

  52: Title 'Gchart Horizontal Bar Chart';

  53: format gender $gender.;

  54:  

  55: Vbar Gender;

  56: Title 'Gchart Vertical Bar Chart';

  57:  

  58: Pie Gender / explode='F';

  59: Title 'Gchart Pie Bar Chart';

  60: run;

 

Note the use of the gout= option to store the plots. Now that the plots have been stored, you will eventually want to view them. Here is some sample code that allows you to view your plots and graphs that are stored in a catalog.

   1: ods html body='c:\demolib\gChartDemo1.html';  

   2:  

   3: options device=gif;

   4: proc greplay igout=graphcat.GenderGraphs nofs;

   5: replay _all_;

   6: quit;

   7: run;

   8:  

   9: ods html close;

 

About the author: Phil Rack is President of MineQuest, LLC. and has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and a reseller of WPS in North America.

WPS Tip – Viewing Graphics Output

When creating graphs in WPS, did you know that to view the chart or plot, you need to do two things. First, add an ODS HTML destination before the creation of the graphics, as well as at the end so that it looks like:

   1: ods html body='c:\battelle\gChartDemo.html';

   2:  

   3: Proc Gchart data=a;

   4:   hbar Gender;

   5:   Title 'Gchart Horizontal Bar Chart';

   6:   format gender $gender.;

   7: run;

   8:  

   9: Proc Gchart data=a;

  10:   Vbar Gender;

  11:   Title 'Gchart Vertical Bar Chart';

  12:   format gender $gender.;

  13: run;

  14:  

  15: Proc Gchart data=a;

  16:   Pie Gender / explode='F';

  17:   Title 'Gchart Pie Bar Chart';

  18:   format gender $gender.;

  19: run;

  20:  

  21: ods html close;

After you submit the code, go to the output tab and you will see selections such as Log, Listing and Results. Click on the Results tab to open that selection and then click HTML. Your plots and charts are viewable from within the Workbench.

About the author: Phil Rack is President of MineQuest, LLC. and has been a SAS language developer for more than 25 years. MineQuest provides WPS and SAS consulting and contract programming services and a reseller of WPS in North America.

Technorati Tags: ,,