Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

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;

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;

Sunday, February 15, 2009

count word number using PRX

It is my way to count word number:

data _null_;
input;
line = prxchange('s/\b*(\w+)\b*/A/', -1, _infile_);
count = count(line, 'A');
put count=;
cards;
this is a test
;

++++++++++++++++++++++++++++++++++++++++++++++++

Apparently, the method above is outdated.
With SAS 9.2, we can count the words in a string easilier.
The functions countw do the trick.

Monday, February 9, 2009

functions for substring searching

Generally, we can use 4 classes of search substring:
ANY-function, NOT-function (anyalpha)
INDEX (INDEXC, INDEXW)
FIND
PRXMATCH


The function family of any-function and not-function are for search of a character string. And they are for special purpose.
Sometimes, the function COMPRESS can used in practical application alternatively.

The power of subsring searching is
INDEX < FIND < PRXMATCH

And certainly, the complexity of usage
INDEX < FIND < PRXMATCH

I prefer PRXMATCH since it has most flexibility. Although it will take long time to learn, it is worth the effort.

Wednesday, January 21, 2009

a special function for %sysfunc world: filename function

Below are the code from SAS online doc:
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf, physical-filename));
%if &rc ne 0 %then
%put %sysfunc(sysmsg());
%let rc=%sysfunc(filename(filrf));

It should be noted that the fileref is "myfile", not "filerf". That means the macro variable "filerf" contains the ACTUAL fileref.
When the macro variable does not exist, the system generates a fileref automatically.

It seems that it is unique to filename function.
For now, I have not found any other functions which can take the similar behavior.

Note that it will not work if the fileref has been assigned. For more information, please see http://support.sas.com/kb/6/567.html

Sunday, January 4, 2009

CALL EXECUTE change the "macro execution path"

As for CALL EXECUTE, SAS Manual says that
"If argument resolves to a macro invocation, the macro executes immediately and DATA step execution pauses while the macro executes. If argument resolves to a SAS statement or if execution of the macro generates SAS statements, the statement(s) execute after the end of the DATA step that contains the CALL EXECUTE routine."

And it is interesting that the macro execution path are different for CALL EXECUTE type and standard type.

Standard type executes Macro language (statement, function, variable) and SAS statements step by step. However, CALL EXECUTE type do NOT execute SAS statement, which are created by macro, but put them after the end of that DATA step.

Sample code:

%macro test(a);
%let b=2;
%put 1> a is "&a" and b is "&b";
data _null_;
put "2> a is '&a' and b is '&b'";
call symput('b', '3');
run;

%put 3> a is "&a" and b is "&b";
%mend;

* Standard Type;
%test(1)

* Output:
1> a is "1" and b is "2"
2> a is '1' and b is '2'
3> a is "1" and b is "3"
;


* CALL EXECUTE Type;
data _null_;
call execute('%test(1)');
run;

* Output:
1> a is "1" and b is "2"
3> a is "1" and b is "2"
2> a is '1' and b is '2'
;

Thursday, December 11, 2008

Interval: Key concept to INTNX and INTCX

When handling dates and times, we must know two powerful functions: INTNK and INTCK.
The two functions are complementary.
INTNK is for new date value incremented by a specified number of intervals, while INTCK for the number of intervals between two dates.

It is hard for a fresh SAS programmer to understand the two functions because they are not intuitive.
However, as a qualified SAS programmer, it should be a must.

Actually, the key concept behind the two functions is INTERVAL.
If you get it, anything will be simple.

INTNX Demo:
INTNX("year", "31Dec2000"d, 2);

As to "Year" interval, "31Dec2000"d is in the interval between "01Jan2000"d and "31Dec2000"d.
So the new interval should be calculated as follow:
"01Jan2000"d and "31Dec2000"d
+
2
= "01Jan2002"d and "31Dec2002"d.
In the new interval, INTNX return date ‘01Jan2002’d. (Note: the default alignment is beginning).

INTCK Demo:
INTCK("year","31Dec2000"d, "01Jan2001"d);
As to ‘Year’ interval, '31Dec2000'd is in the interval between "01Jan2000"d and "31Dec2000"d’,
While "01Jan2001"d in the interval between "01Jan2001"d and "31Dec2001"d

The number of interval is:
"01Jan2001"d and "31Dec2001"d
-
"01Jan2000"d and "31Dec2000"d’
= 1

So INTCX return number 1.


One more thing:
SAS always focus on improvement of "The Power to DO". (I have modified the logo. Wow.)
With new introduction of alignment value ‘sameday’ in INTNK in SAS 9, many old solutions will step off the stage of history.