The correct way of hardcoding

Sometimes even after a good attempt by clinical data management at cleaning and coding the data, you may still find the data contain some undesired values. Therefore, you may need to use hardcoding to override the data before you have time to fix them in data management system.

However, hardcoding is dangerous and it is better to avoid hardcoding in any circumstance. One big reason is that data often change over time and the hardcoding writing today may not be appropriate in the future. A hardcode can be easily forgotten and the left code normally will lead to an unpredictable error when you analyze the data.

If hardcoding must be done, some programming skills may be helpful to reduce that risk. See the example below, the &sysdate was used to force the hardcoding to expire at some date point.

 

data test;
  set test;
  * Hardcode approved by Someone on 12/13/2012;
  if identity = "NEMISIS" and "&sysdate"d <= "13Dec12"d then do;
    ....;
    ....;
  end;
run;

 

 

How to get the data set variable list into a macro variable

Sometimes when you have a huge SAS dataset and would like to list or print the variable names in the dataset, it is better to store the list of variable names into a macro variable first and then you can use this macro variable to either print or select the specific columns which you wanted.

There are multiple ways to do this, for example using PROC CONTENTS or a better way below:

proc sql noprint; 
 select distinct name 
 into : varlist separated by ' '
 from dictionary.columns
 where upcase(libname)='WORK' and 
       upcase(memname)='Your-data-set-name';
quit;

Invisible Character Alt-255

The text aligning and positioning in SAS output is really important if you want your report looks good. I usually use space to aligning text in titles, footnotes and columns, etc. However, SAS have its own rule to handle the blanks, especially the leading or trailing blanks, so sometimes the space cannot do what you want.

Here I’m introducing a new simple and elegant approach: using Alt-255. It looks like a blank space in the program code and SAS output but is processed and printed by many programs as a valid text character.

Now, how? First of all, remember you need to use a numeric keypad for typing the magic number 255.

You should follow the following steps to create an invisible Character.

1. Press and hold the “Alt” key and while holding it, type digit keys 255 from numeric keypad.
2. Release the “Alt” key and after releasing the cursor will moves to the next position so you will know that an invisible character has been inserted.

Actually, we can use Alt-N to enter any letter and a lot of graphical symbols. There is a nice place where you can check all Alt-N characters (http://www.alt-codes.net/). Alt-255 is of special interest just because it is invisible.

See the example below:

data test;
  input fname $;
* The blank before Alan is Alt-255, before Andy is space;
datalines;
Joe
 Alan
 Andy
;
run;

proc print data=test;
run;

And the result:

1 Joe
2  Alan
3 Andy

Remove all labels and formats in SAS data set

I occasionally find that the labels in data set are annoying especially when this data set is from outside (means someone else create the data set). The labels will cover the variable names when you check the data and therefore you may incorrectly use the label instead of true variable name in programming. And It normally waste me much time to debug.

There is a very easy way to remove all labels in a single step:

* Remove all the labels and formats in data set;
proc datasets lib=work memtype=data;
  modify data_set_name;
  attrib _all_ label='';
  attrib _all_ format=;
run;

Hope it also helps you !

A smart way to comment chunks of code in SAS

We all know there are two styles of comments in SAS: * ; and /* */. Normally when we want to disable a chunk of code, we will choose /* */.

But I bet you have such experience that you cannot do it well with /* */ since parts of the code itself might contain /* */ style comments. Therefore, in this case, only the code up to the first */ would be commented. So the best method to disable a chunk of code is to put it in a macro declaration and never call the macro, for example:

%macro comment;
---
---
---
%mend comment;

 

 

Use the IN operator in macro

The IN operator is one convenient checking tool in SAS which can be used in place of lots of OR statements. And it’s quite common in data step.

data test;
  if 3 in (1, 2, 3, 4, 5, 6) then
    put "Value found within list.";
  else put "Value not in list.";
run;

Sometimes, you will come across a situation where you have to write a macro program where a macro variable has more than one value. You may think to use multiple OR operators as below…

* Prior to SAS9.2, the following syntax was used;
%macro test(value); 
  %if &value=1 or &value=2 or &value=3 or &value=4 or
      &value=5 or &value=6 %then %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

Starting SAS 9.2, there is an IN operator for the macro language. The IN operator can now be used on the %IF statement when the MINOPERATOR option is set on the %MACRO statement or as a SAS system option.

%macro test(value)/minoperator;
  %if &value in 1 2 3 4 5 6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

Or you can use character # (new binary comparison operator) as an alternate operator to mnemonic IN operator.

%macro test(value)/minoperator;
  %if &value # 1 2 3 4 5 6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

They both work fine. MINOPERATOR option tells SAS to recognize the word ‘IN’ or # by the SAS macro facility as a logical operator in expressions.

And there is another way of writing code which is more close to the way we use IN operator in data step. By default, a space is used as the delimiter for the operator, but can be changed by using the MINDELIMITER= %MACRO statement option.

options minoperator;

%macro test(value)/mindelimiter=',';
  %if &value in 1,2,3,4,5,6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

 

Reference: 

http://support.sas.com/kb/35/591.html

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a003092012.htm

Use SAS system options to suppress Log output

For Windows SAS system, the Output, Log, and Program Editor windows can display approximately 99,999 lines each. Since the number is limited, sometimes the SAS Log windows will be full and the system will show a messages ‘SAS log window is full’. The running program will pause and wait for your action. It’s quite annoying, especially when you run a program with hundreds or thousands of loops.

I run a simulation program with a loop that went 100,000 times recently and it took more than 4 hours to run, even on the sever. And I want to suppress the log output because I don’t want that message to interrupt the program running.

I read the SAS documents carefully, and there are several option available to prevent the log window from filling up.

Solution 1: System options

There are four system options can be used to suppress SAS statements, system messages, and error message, respectively.Continue reading