Entries in Programming (23)
Proc SQL Resource
I find I am using Proc SQL more and more. SAS 9.2 Documentation has some great examples of using Proc SQL. Of note is the Practical Problem-Solving with Proc SQL.
Reordering Variables in SAS
My data is using Variable Range Lists so I want to ensure my input data is in the correct order. According to fellow SAS programmer Warren Repole, the old way was to initialize variables using the LENGTH statement. Example:
data neworder_length;
length Name $ 6 Age 8 Sex $ 1 Weight Height 8;
set sashelp.class;
run;
However, it's annoying to have to know the variable types and lengths. Instead, use the new way (for version 8.2 and up) by using the RETAIN statement. Example:
data neworder_retain;
retain Name Age Sex Weight Height;
set sashelp.class;
run;
All variable attributes are kept and no data is lost. You only need to know the variable names.
Warren points out this new way is "...not suitable when new variables are created during the DATA step because this technique partially relies on the automatic retain behavior for variables read through the SET statement. That retain behavior may be inappropriate for the values of newly created variables. [The FORMAT statement is a potential alternative for new variables.]"
Thanks Warren!
(Read his original post here)
Mystery of Proc Tab Inserting the Word "And"
In a response to my own thread on the SAS Google group, my supervisor and I found a solution to the appearance of the mysterious word "and" in our Proc Tabulates.
The answer came from the SUGI paper Anyone can Learn Proc Tabulate which discussed the _PAGE_ variable and Three dimensional proc tabs.
In Three dimensional Proc Tabs, if you don't supply 'labels' for the Page dimension, it will simply print out their values. See this following example which prints the values for "origin" and "make" on the top left:
PROC TABULATE DATA=sashelp.cars noseps;
CLASS make type origin drivetrain;
table origin = ' ' * make = ' ',
type = ' ' , drivetrain = ' ' ;
RUN;
Now if we add a label (e.g. origin = 'Origin is equal to ') then SAS puts an "and" between origin and make:
PROC TABULATE DATA=sashelp.cars noseps;
CLASS make type origin drivetrain;
table origin = 'origin is equal to = ' * make = 'make is equal to = ',
type = ' ' , drivetrain = ' ' ;
RUN;
In the top left you should see this heading:
origin is equal to = USA
and make is equal to = Saturn
My original question was if the word "and" could be removed while keeping both the custom label and printed value. I still don't know the answer, but I sure learned a lot about Proc Tabulate!
How to delete observations containing characters
The SAS Google Group had a question on how to delete observations containing characters (e.g. 92z89 or abcd) and only keep the ones with numeric values.
The thread was hijaked by another topic, but there were a number of good options submitted. Here is a summary of the suggestions:
data mydata;
input value $;
cards;
1023442
92z89
abcd
5231295
09CX42
9e122
12E3
98722
;
run;
*Suggestion #1 - simple deleting;
*Keep only numeric values;
data want1; set mydata;
if anyalpha(value)>0 then delete;
run;
*Suggestion #2 - using PRXMATCH function;
data want2; set mydata;
if prxmatch("/[a-zA-Z]/",value)=0;
/* Searches for a pattern match and returns the position at which the pattern is found */
run;
*Suggestion #3;
/*
Do you want to delete the letters?
Also, it's not a good idea to just simply delete data without knowing the value. You may find you want to keep
those observations. So why not try using the New Compress function in SAS9 or above and create a new variable?
*/
data want3a; set mydata;
var = Compress (value, ,"Kd") ;
*Kd is keep digits;
run;
*SAS 8;
data want3b; set mydata;
var = Compress (value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
run;
*Suggestion #4;
/* How about 12e3? It contains an alpha character but is nevertheless valid as
the external representation of a numeric value?
If so, you need to keep numbers stated in
scientific notation, then try the following method:*/
%LET N_Errors = %SYSFUNC(GETOPTION(Errors));
OPTIONS ERRORS=0;
data want4; set mydata;
if missing(input(value,best12.)) then delete;
OPTIONS ERRORS=&N_Errors;
run;
SAS Titles - how to get a blank line between titles
How dumb. I've been trying for 30 minutes to figure out how to get a blank line (an empty line) between two of my TITLES in my ODS RTF output. All you have to do is skip over a TITLE statement!
In this example, just avoid using title2 to get an empty line between title1 and title3:
title1 'This is my main header';
title3 'This is my sub header';
And your output will be:
This is my main header
<empty line>
This is my sub header
The SAS Docs have a nice section on Titles, Footnotes, and Notes.

