Technology Tales

Notes drawn from experiences in consumer and enterprise technology

Calculating geometric means using SAS

19th March 2012

Recently, I needed to calculate geometric means after a break of a number of years and ended racking a weary brain until it was brought back to me. In order that I am not in the same situation again, I am recording it here and sharing it is always good too.

The first step is to take the natural log (to base e or the approximated irrational value of the mathematical constant, 2.718281828) of the actual values in the data. Since you cannot have a log of zero, the solution is either to exclude those values or substitute a small value that will not affect the overall result, as is done in the data step below. In SAS, the log function uses the number e as its base, so you need to use the log10 equivalent when base 10 logs are needed.

data temp;
    set temp;
    if result=0 then _result=0.000001;
    else _result=result;
    ln_result=log(_result);
run;

Next, the mean of the log values is determined, and you can use any method of doing that so long as it gives the correct values. PROC MEANS is used here but PROC SUMMARY (identical to MEANS except it defaults to data set creation while that generates output by default; for that reason, we need to use the NOPRINT option here), PROC UNIVARIATE or even the MEAN function in PROC SQL.

proc means data=temp noprint;
    output out=mean mean=mean;
    var ln_result;
run;

With the mean of the log values obtained, we have to take the exponential of the obtained value(s) using the EXP function. This returns values of the same magnitude as in the original data, using the formula emean.

data gmean;
    set mean;
    gmean=exp(mean);
run;

Creating placeholder graphics in SAS using PROC GSLIDE for when no data are available

18th March 2012

Recently, I found myself with a plot to produce, but there were no data to be presented, so a placeholder output was needed. For a listing or a table, this is a matter of detecting if there are observations to be listed or summarised and then issuing a placeholder listing using PROC REPORT if there are no data available. Using SAS/GRAPH, something similar can be achieved using one of its curiosities.

In the case of SAS/GRAPH, PROC GSLIDE looks like the tool to user for the same purpose. The procedure does get covered as part of a SAS Institute SAS/GRAPH training course, but they tend to gloss over it. After all, there is little reason to go creating presentations in SAS when PowerPoint and its kind offer far more functionality. However, it would make an interesting tale to tell how GSLIDE became part of SAS/GRAPH in the first place. Its existence makes me wonder if it pre-exists the main slideshow production tools that we use today.

The code that uses PROC GSLIDE to create a placeholder graphic is as follows (detection of the number of observations in a SAS dataset is another entry on here):

proc gslide;
    note height=10;
    note j=center "No data are available";
run;
quit;

PROC GSLIDE is one of those run group procedures in SAS so a QUIT statement is needed to close it. The NOTE statements specify the text to be added to the graphic. The first of these creates a blank line of the required height for placing the main text in the middle of the graphic. It is the second one that adds the centred text that tells users of the generated output what has happened.

Creating waterfall plots in SAS using PROC GCHART

17th March 2012

Recently, I needed to create a waterfall plot couldn't use PROC SGPLOT since it was incompatible with publishing macros that use PROC GREPLAY on the platform that I was using; SGPLOT doesn't generate plots in SAS catalogues but directly creates graphics files instead. Therefore, I decided that PROC GCHART needed to be given a go, and it delivered what was needed.

The first step is to get the data into the required sort order:

proc sort data=temp;
    by descending result;
run;

Then, it is time to add an ID variable for use in the plot's X-axis (or midpoint axis in PROC GCHART) using an implied value retention to ensure that every record in the dataset had a unique identifier:

data temp;
    set temp;
    id+1;
run;

After that, axes have to be set up as needed. For instance, the X-axis (the axis2 statement below) needs to be just a line with no labels or tick marks on there and the Y-axis was fully set up with these, turning the label from vertical to horizontal as needed with the ANGLE option controlling the overall angle of the word(s) and the ROTATE option dealing with the letters, and a range declaration using the ORDER option.

axis1 label=none major=none minor=none value=none;
axis2 label=(rotate=0 angle=90 "Result") order=(-50 to 80 by 10);

With the axis statements declared, the GCHART procedure can be defined. Of this, the VBAR statement is the engine of the plot creation, with the ID variable used for the midpoint axis and the result variable used as the summary variable for the Y-axis. The DISCRETE keyword is needed to produce a bar for every value of the ID variable, or GCHART will bundle them by default. Next, references for the above axis statements (MAXIS option for midpoint axis and AXIS option for Y-axis) are added, and the plot definition is complete. One thing that has to be remembered is that GCHART uses run group processing, so a QUIT statement is needed at the end to close it at execution time. This feature has its uses and appears in other procedures too, though SAS procedures generally are concluded by a RUN statement.

proc gchart data=temp;
    vbar id / sumvar=result discrete axis=axis2 maxis=axis1;
run;
quit;

Do we need dongles anymore?

12th March 2012

Recent exposure to both the HTC Wildfire S and the HTC Desire S has me wondering if we need mobile broadband dongles any more. The reason for my asking this is that both Android devices can act as mobile Wi-Fi hubs, and they work very well as these too. Even the dedicated T-Mobile mobile Wi-Fi hub that I picked up in the closing months of 2011 now looks a little obsolete, though it retains a cost advantage in its favour.

In the case of both HTC phones, it thankfully is possible to use high security encryption and a pass key too. However, it is best to change the default key before any activation of Wi-Fi signal, if only to ensure that you don't end up with a very nasty bill. The Wi-Fi Hotspot App has all the settings that you need and up to five connections can be supported at a time, just like that T-Mobile hub that I mentioned earlier.

That replaced a defunct dongle from the same company; the USB connection appeared to have failed and was ailing for a while. Now, it might be that the more transparent use of an actual mobile phone might be usurping that as well, especially when I have been wondering why it has been doing so well with internet connectivity while using it on the move. This voyage into the world of what smartphones and similar devices can do is throwing up its share of surprises as I go along.

Smoother use of more than one SAS DMS session at a time

11th March 2012

Unless you have access to SAS Enterprise Guide, being able to work on one project at a time can be a little inconvenient. It is possible to open up more than one Display Manager System (DMS, the traditional SAS programming interface) session at a time only to get a pop-up window for SAS documentation for the second and subsequent sessions. You don't get your settings shared across them, either, while also losing any changes to session options after shutdown.

The cause of both of the above is the locking of the SASUSER directory files by the first SAS session. However, it is possible to set up a number of directories and set the -sasuser option to point at different ones for different sessions.

On Windows, the command in the SAS shortcut becomes:

C:\Program Files\SAS\SAS 9.1\sas.exe -sasuser "c:\sasuser\session 1\"

On UNIX or Linux, it would look similar to this:

sas -sasuser "~/sasuser/session1/"

Since the "session1" in the folder paths above can be replaced with whatever you need, you can have as many as you want too. It might not seem much of a need but synchronising the SASUSER folders every now and again can give you a more consistent set of settings across each session, all without intrusive pop up boxes or extra messages in the log too.

  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.