<img alt="" src="https://secure.perk0mean.com/171547.png" style="display:none;">

Your SAS Secrets Exposed! [4 SAS Tips]

SAS_Tips

During my time in the life science industry I have learnt a lot of SAS techniques through attending training sessions, however some of the best SAS tips I have picked up were from other programmers: for instance when asking for advice on a coding problem or running programs written by colleagues. I found there are many simple SAS® tips you can use in your day to day SAS programming. This blog will provide explanations and examples of four of these.

 

The following four SAS® tips will be reviewed: 

  • Using the functions IFN and IFC as an alternative to IF THEN ELSE statements in conditional processing.
  • How to find out and use special characters which cannot be seen on your keyboard.
  • Making use of the ‘Where’ option in the data menu to find outliers and data issues.
  • Utilising the handy keyboard shortcuts available in PC SAS.

 

SAS TIP 1: ALTERNATIVE TO IF THEN ELSE

Most programmers will have used the IF THEN ELSE statements when working with conditional processing. Assigning one value for when the condition is true, and another value for when the condition is false. In these situations you can reduce lines of code by instead using the functions IFC and IFN.

These functions can both create a new variable with assigned values based on whether a condition is true or false. IFC will create a character variable and IFN will create a numeric variable.

Below are standard templates for using IF THEN ELSE to create numeric/character variables, along with their counterpart in IFN and IFC:

*Standard IF THEN ELSE code (numeric);

if condition then variable=value1;

else variable=value2;

 *Equivalent IFN code;

variable=ifn(condition,value1,value2);

 *Standard IF THEN ELSE code (character);

if condition then variable='value1';

else variable='value2';

 *Equivalent IFC code;

variable=ifc(condition, 'value1' , 'value2') ;

 

You can see that IFN and IFC effectively cut down and rearrange the keywords from IF THEN ELSE code and change it into just one compact line of code.

For example both the following sets of code can be used to create the same variable:

 

if sex='Male' then sexcd=1;

else sexcd=0;

 

 

sexcd=ifn(sex='Male',1,0);

arrows table_1

 


Either code can be used here and will produce the same result. In order to compare processing times I ran equivalent code five times for each method and recorded the average times. These were run within server SAS version 9.1.3 and showed very little difference in average processing times. However the IFN function has the advantage of requiring less code.

These functions are ideal when you wish to create variables with 2 possible values.



SAS TIP 2: ACCESS SPECIAL CHARACTERS



There may be times when you need a specific character used in your outputs, for example superscripts or trade mark symbols in a footnote, but cannot see these directly on your keyboard. These characters and many more are all accessible for use in your SAS programs.

This table shows some of the special characters available to use, along with their corresponding byte number in SAS and a description.

 

Byte(i)

Symbol

Description

153

Trademark sign

169

©

Copyright sign

170

ª

Superscript a

174

®

Registered trademark sign

176

°

Degree symbol

177

±

Plus or minus sign

178

²

Superscript 2 / squared

179

³

Superscript 3 / cubed

185

¹

Superscript 1

188

¼

One quarter

189

½

Half

190

¾

Three quarters

247

÷

Division sign

 

 

The full list of characters can be seen by running the following code. This will create a dataset with a variable containing each special character and a variable with each respective byte number in SAS.

 

data check;

  do i=1 to 255;

    sq=byte(i);

    output;

  end;

run;

 

Once you know which character you want in your program, then you can use %let to create a macro variable of it. For example the following code will create a macro variable for the superscript a symbol:

 

%let supa=%sysfunc(byte(170));

 

If you want to check this has worked properly, then use %put to write the value of this macro variable to the log:

%put &supa;

 

The macro variable can then be inserted into your program. For example the following is standard proc report code for creating a table, but using this macro variable to create a superscript value in the footnote. The highlighted code shows where the macro variable is used.

 

proc report data=final nowd headline headskip split=' | ' ps=23 formchar(2)='_';

  column ('__' pdcdl col1 ord1 ord2);

  define pdcdl / left  '  ' width=60;

  define col1 / center 'DrugA|6 mg SC';

  define ord1 / order noprint;

  define ord2 / order noprint;

  title 'Table 14-1.3 Summary of Important Protocol Deviations';

  compute after _page_;

    line @13 72*"_";

    line @2 ' ';

    line @14 "&supa Other than Treatment Compliance/Test Article Administration";

    line @14 'Note: Deviation categories are not mutually exclusive';

  endcomp;

  break after ord1 / skip;

run;

 

This will create the following output. The highlighted part shows what the footnote looks like from inserting the special character into it.

 

table_2

 


As you can see in the example, it is a simple process to find out the byte number of the desired special character and then use it in your program.

 


SAS TIP 3: ‘WHERE’ OPTION

 


Often in your datasets you may find issues, outliers and unexpected values, which sometimes may not be noticed straight away. There is a quick way to see all the unique values of a variable in a dataset by using the where option from the data menu.

Although its primary use is to subset the data, it is also ideal for getting an overview of what values a variable can take.

The following steps show an example of how to use this option:

 

1) Open a dataset and select 'Where...' from the 'Data' menu

 

table_3

 

2) Choose the variable you want to check

 

table_4

 

3) Select the EQ operator 

table_5

 

4) Select <LOOKUP distinct values>

 

table_6

 

5) List of values is now visible

 

table_7

 

Using this option will then show the unique values of that variable, sorted in ascending order. This makes for an easy way to get an overview of the data.

For example in your demographics dataset you could look at all the values of age. For a numeric variable like this, you will get to see the minimum and maximum values in the data and also get an idea of possible outliers.

 

table_8

 

Another example is looking at start dates in your adverse event dataset, as these can be subject to a lot of issues. Looking at this variable would let you see any incorrect / unusual values which will cause problems when programming.

 

table_9

 


Having this quick way of looking at data values can be great for saving time when wanting to check for issues, outliers and unexpected values.



SAS TIP 4: PC SAS SHORTCUTS



When you are working on PC SAS there are many helpful keyboard shortcuts available at your disposal. For example when you need to comment or un-comment blocks of code, you can save time by using the shortcut instead of typing it all yourself. Or when you are trying to find the matching pair in a set of brackets, simply using the shortcut means that you will not need to spend the time searching yourself.

These actions can be performed with ease by pressing a few buttons on the keyboard. The following table shows some of the shortcuts available to use:

 

Action

Keyboard Shortcut

Convert highlighted text to upper case

Ctrl Shift U

Convert highlighted text to lower case

Ctrl Shift L

Comment highlighted text

Ctrl /

Uncomment highlighted text

Ctrl Shift /

Collapse all sections of code

Ctrl Alt – (on number pad)

Expand all sections of code

Ctrl Alt + (on number pad) 

Move cursor to matching bracket

Ctrl (

Move cursor to matching DO or END statement

Alt [

 

The full list of available shortcuts can be accessed through the menu:
Tools -> Options -> Enhanced Editor Keys

This will show a list of all commands that have a keyboard shortcut assigned to them, along with a brief description of what the command will do.

 

table_010

 

 

Ticking the box Show all commands will then also show the commands that have not been assigned any shortcuts yet. These can then be set yourself if you want to use any. For example here are some of the unused commands which can be set a shortcut:

  • Convert highlighted text to opposite case
  • Insert current date and time
  • Delete line
  • Repeat the current line
  • Remove trailing white space from end of lines

The Assign keys… button can be used to set a shortcut or to change an existing shortcut.

Personally I find switching text to upper/lower case and commenting/un-commenting code to be especially useful and a good time saver.

 


CONCLUSION

These tips are all easy to learn, quick to use and great to share with other programmers. I would recommend trying these out to see if they can help you in your day to day SAS programming.



This content was originally written for PhUSE 2013 coders corners stream.

 

 Data Efficiency in Clinical Trials

 

Subscribe to the Blog