Pages

Wednesday, July 31, 2013

DATECOPY option in PROC DOWNLOAD

The DATECOPY option in PROC DOWNLOAD can be used when migrating SAS datasets to preserve the original create date and time on the SAS dataset.

DATECOPY also works with PROC UPLOAD, PROC COPY and the COPY statement in PROC DATASETS.

proc download data=perm.test
out=out.test datecopy;
run;

proc datasets lib=source;
copy out=dest datecopy;
quit;

Bookmark a code to find it later

If you are working with long codes, you may want to bookmark the code. Here is the Tip

You can bookmark a line of code in your Program Editor to make it easier to find later.

To bookmark a line, press Ctrl + F2 on the line you want to bookmark. A vertical rectangle will appear in the margin to indicate that the line is book marked.

To go directly to a book marked line, press F2. To go to a previous bookmark, press Shift + F2.
To remove a bookmark, go to that line and press Ctrl + F2.

Merging Datasets faster

When merging two SAS data sets, many of us use one of these two methods
to keep only the observations where both input data sets contribute:

data test1;
merge step1(in=a)
step2(in=b);
by district studentid;
if a=1 and b=1;
run;
or
data test1;
merge step1(in=a)
step2(in=b);
by district studentid;
if a and b;
run;

This method executes faster (and saves a few keystrokes):
data test1;
merge step1(in=a)
step2(in=b);
by district studentid;
if a*b;
run;

This shortened version of the IF statement works since if either the variable
a or b is false, the expression resolves to zero (false).

Monday, July 29, 2013

How to comment in SAS?

In the PC-SAS enhanced editor it's easy to enclose a block of code or text within a block comment. Just highlight the selected code and press Ctrl + /

To remove a block comment, highlight the selected code and press 
Ctrl + Shift /

Shortcut for Listing Variables

You can keep all of the numeric or character columns when creating a new data set without listing each one in your KEEP statement:
data test1 (keep = _numeric_); run;
data test2 (keep = _character_); run;

Example:
The work.source data set has three character and five numeric variables. This keep statement keeps one character variable (district) and all of the numeric variables:

data work.mix(keep= distname _numeric_);
set work.source;
run;

This method can also be applied to drop, sum, arrays, etc.

Using Wildcards in Variable Lists

You can use the colon as a variable modifier (e.g. abc:), which acts as a wildcard. So rather than entering variables abc1, abc3 and abcde you could just specify abc:. It is great for saving typing long variable lists.

The colon must be at the end, it can’t be embedded (e.g. ab:c).

Using The SUM Function to Prevent Missing Values in SAS

You can use the SUM function to prevent missing values if one argument has a missing value.

The syntax for using the SUM function to create a new variable is:
newvar=sum(var1, var2);
The SUM function ignores missing values, so if var1 and var2 are both missing values then the value of newvar will also be missing.

You can prevent that by coding in a constant zero:
newvar=sum(var1, var2, 0);

In the following example:
1. A data set is created by reading in three variables: var1, var2 and var3.
Note that the third observation has missing values for var1 and var2.

2. The SUM function is used to create newvar1, which is the sum of var1 and var2.

3. The SUM function is also used to create newvar2 by summing var1 and var2, but it also codes in a constant zero.

data step1;
input @1 var1 1-2
@4 var2 4-5
@7 var3 7-8
;
newvar1=sum(var1, var2);
newvar2=sum(var1, var2,0);
datalines;
10 20 15
10 10 25
35
20 20 45
;
run;

proc print data=step1;
var var1 var2 newvar1 newvar2;
run;
quit;


For the third observation, there is a missing value for newvar1. But by including a zero in the SUM statement for newvar2, the value of the third observation is zero instead of missing.

Where OR IF in SAS?

When is it most efficient to use a WHERE statement vs. a subsetting IF statement? The answer is (of course) it depends.

When subsetting from a large dataset, WHERE processing is usually more efficient because WHERE statements are evaluated before an observation is read into the program data vector. Subsetting IF statements test the condition after an observation is read into the program data vector.

However, you cannot use WHERE processing on anything other than a variable value that already exists.

Subsetting IF statements must be used in expressions that use:
• Values from raw data
• Values calculated or assigned during the course of the data step
• Automatic variables created in a data step (ex: first.variable , last.variable, _N_)

WHERE expression examples:
proc print data=work.enrollment;
where district='227913';
run;
proc print data=work.enrollment (where=(birthdate = '01SEP2000'D));
run;

proc sql;
select district, sum(students) as Students
from work.enrollment
where district='227901';
quit;

Debugging a complex macro

How to debug a complex macro?

You can write code generated by macros to an external file. Since you can see the code that is generated, this technique can be useful for debugging complex macros.

The MPRINT system option writes to the SAS log each SAS statement generated by a macro. Using the MPRINT option is recommended when you suspect your bug lies in code that is generated in a manner you did not expect.

The MFILE option specifies whether MPRINT output is directed to an external file.

Using the technique described below, you can redirect the SAS statements generated by a macro to an external file instead of to the log.
First, include MFILE and MPRINT in your options statement:
options mfile mprint;
Next, include a fileref for MPRINT:
filename mprint '/home/mydir/mymacro.sas';

Saturday, July 27, 2013

What is the difference between Count, Count C and Count W?

Let us see with example a difference between Count, Count C and Count W in SAS:


DATA ds;
text = 'we are learning SAS function like booom boom';

noofwrd = COUNTW(text);
noofstr = COUNT(text,'boo');
noofchar = COUNTC(text,'bo');
RUN;

/* Result explanation */;

COUNTW
------
noofwrd = 8, because there are 8 words found in the string 'we are learning SAS function like booom boom'. So COUNTW
counts word in a string.

COUNT
-----
noofstr = 2, because there are 2 'boo' found in the string 'we are learning SAS function like booom boom'. So COUNT counts
how many times specific character/s is/are occured in a string.

COUNTC
------
noofchar = 8, because there are 2 'b' and 6 'o' (2+6=8) found in the string 'we are learning SAS function like booom boom'. So
COUNTC counts how many time specified character/s is/are occurred in a string. Difference of COUNTC with COUNT function is that COUNTC would look for 'b' and 'o' anywhere in the string and it does not need to be 'bo' in the string to be counted.

Friday, July 26, 2013

Reading Text from a zip file


There is a currently undocumented (and unsupported) filename engine that can be used to read text from compressed ZIP files directly. You use it by specifying the engine "SASZIPAM" on a filename statement that points to the zip file.

When referring to it you specify the file within it that you wish to read. The contents of the compressed file are written to your SAS log.

In this example, the zip file "STT.zip" contains several text files. I want to read "sweep.txt" and therefore specify "zipfile(sweep.txt)", where "zipfile" is the fileref.
filename zipfile saszipam 'C:\temp\STT.zip';

data _null_;
infile zipfile(sweep.txt);
input;
put _infile_;
run;

How to check for the existence of a variable in SAS?

If you want to check if a variable exists in a dataset or not, you can simply use the below macros which uses open and varnum function, to open a dataset   and check for the presence. Varnum function returns the variable's position in a dataset. So it its vale is greater than 0 then the variable is present.


Below is the macro:

*************************************************************
%macro VarExist(ds,var);
 %local rc dsid result;
 %let dsid=%sysfunc(open(&ds));
 %if %sysfunc(varnum(&dsid,&var)) > 0 %then %do;
  %let result=1;
  %put NOTE: Var &var exists in &ds;
 %end;
 %else %do;
  %let result=0;
  %put NOTE: Var &var not exists in &ds;
 %end;
 %let rc=%sysfunc(close(&dsid));
 &result
%mend VarExist;
*************************************************************