Monday, November 26, 2012

to gzip output using FIFO

Generally SAS programmer make an output file and then run "gzip" command using "X", "CALL SYSTEM" or "%SYSEXEC". Actually, we can gzip output on the fly.
See below:
x "mknod pipe&sysjobid p";

filename cmd pipe "gzip < pipe&sysjobid 
> sashelp.DAT.gz &";

data _null_;
 infile cmd;
 stop;
run;

data _null_;
 file "pipe&sysjobid";

 retain header;
 if missing(header) then do;
  put "NAME|SEX";
  header = 1;
 end;

 set sashelp.class;
 put name +(-1) '|' sex;
run;
filename cmd clear;

x "rm pipe&sysjobid";

Friday, February 25, 2011

Search at one click

I always search something as follows:
1. go to http://support.sas.com/
2. input keyword in blank search box, e.g. proc format
3. select Documentation in "Search support.sas.com"
4. Click search button

It becomes boring when I always do the same steps.

Here is a tip:
1. install CleverKeys for Windows at http://www.cleverkeys.com/ck.html?p=home&os=windows
2. Right click CleverKeys -> Preference -> Web links -> Add new link at below
Description: SAS Document
URL before: http://support.sas.com/dsearch?Find=Search&ct=5210&qt=
URL after: &col=suppprd&nh=10&qp=&qc=suppsas&ws=1&qm=1&st=1&lk=1&rf=0&oq=&rq=0

Of course, you can add any other useful search engines.
My favorite links are Wikipedia, Google and SAS Document.

Now you can search the SAS at one click. :)

Wednesday, July 14, 2010

Delete observations when all variabls are missing

It is a common question.

With the new function CMISS in 9.2, we can perform the task in ONLY one DATA step.
Please see the sample code at below:

data raw;
output; * obs with missing values;
a=1; b='2'; c=.;
output;
run;

data output;
set raw;
array aa {*} $ _character_;
array nn {*} _numeric_;

if cmiss(of _all_) = (dim(aa)+dim(nn)) then delete;
run;

Monday, July 5, 2010

Quick tip: %EVAL

Essentially, the value of all macro variables is character string.

Sometimes, we do need that macro variable has only numeric value.
We can use %EVAL to judge if the string is numeric value.


* Sample;
%let mv=1+a;

%let a=%eval(%scan(&mv, 1, +));
%let b=%eval(%scan(&mv, 2, +)); * Error occurs;

Thursday, June 3, 2010

How to identify the header of ACROSS columns in PROC Report

When one across item is used with >1 analysis items, the header text of the across item will affect all. To use different header text, we have to specify an alias for the across item.
Below are the sample codes:

* Default;
proc report data=sashelp.class nowd;
column sex,height sex,weight;
define sex / across;
define height / analysis mean;
define weight / analysis mean;
run;

* To use alias for the across item;
proc report data=sashelp.class nowd;
column sex,height (sex=sex_alias),weight;
define sex / across "Height";
define sex_alias / across "Weight";
define height / analysis mean " ";
define weight / analysis mean " ";
run;

Monday, May 10, 2010

How to compare two strings?

Are you still using LEFT(UPCASE(...)) =: "XXXX"?

Stop it.
In SAS 9, we can use the function COMPARE. With the modifiers ("i", "l", ":"), we can handle the string comparison easily.

* COMPARE function Demo;
data _null_;
var = " This is test";
if left(upcase(var)) =: 'THIS' then put "Matched (1)";
if compare(var, 'THIS', 'il:') = 0 then put "Matched (2)";
run;

Wednesday, April 21, 2010

Re: Resolve a macro variable within single quotes

Frankly, it is a dup with new SAS sample code at http://support.sas.com/kb/25/076.html.
However, the sample looks not good. It seems that the author do not know exactly the macro quoting functions (%str, %nrbquote, and etc.).

With the code at below, the reader can get the meaning of macro quoting better.

%let name=Fred;

/* resolve the MV at compilation time */
%put %str(%'&name%');

/* resolve the MV at execution time */
/* Please use %nrbquote or %bquote, NOT %quote. */
%put %unquote(%nrbquote('&a'));