Tuesday, September 22, 2009

Resolve Function

%let first=Steve;
%let last=Hanks;

data _null_;
      name1='Mr '||"&first"||' '||"&last";
      name2=resolve('Mr &first &last');
      put name1;
      put name2;
run;

105 data _null_;
106 name1=‘Mr ’||”&first”||’ ’||”&last”;
107 name2=resolve('Mr &first &last');
108 put name1;
109 put name2;
110 run;

Mr Steve Hanks
Mr Steve Hanks

Using %SYSFUNC Functions

%let dsid=%sysfunc(open(datamart.customer)) ;
%let nobs=%sysfunc(attrn(&dsid,nobs)) ;
%let dsid=%sysfunc(close(&dsid)) ;

%put The number of obs in datamart.customer is &nobs ;

81 %let dsid=%sysfunc(open(datamart.customer)) ;
82 %let nobs=%sysfunc(attrn(&dsid,nobs)) ;
83 %let dsid=%sysfunc(close(&dsid)) ;
84 %put The number of obs in datamart.customer is &nobs ;
The number of obs in datamart.customer is 6299497


Using %SYSFUNC For Report Date

proc print data=sashelp.class;
      title "We can put a todays (%sysfunc(today(),date9.)) date in a report title";
run;

SYMGLOBL and SYMLOCAL

NOTE: Macro variable created inside the macro do loop is local, but can be changed to global by adding %global var after %do ...

%let c=yes;

%macro test;

%let d=yes;

data _null_;
      if symlocal ('c') then put '**** c is local';
      if symglobl('c') then put '**** c is global';
      if symlocal ('d') then put '**** d is local';
      if symglobl('d') then put '**** d is global';
run;

%mend test;
%test;

**** c is global
**** d is local

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

Deleting a Macro Variable

NOTE: symdel will NOT work on local macro variable.

%let a=yes;

data _null_;
      if symexist('a') then put '**** a exists';
          call symdel('a');
      if symexist('a') then put '**** a still exists';
          else put '**** a doesnt exist';
run;

**** a exists
**** a doesnt exist

NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

SYMEXIST to check if a Macro Variable Exists

%let a=1;

data _null_;
      if symexist('a') then put '**** a exists';
          Else put '**** a doesnt exist';
      if symexist('b') then put '**** b exists';
          Else put '**** b doesnt exist';
run;

**** a exists
**** b doesnt exist

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

Monday, September 7, 2009

Macro IN Operator (SAS 9.2)

In SAS 9.2 version:

%macro test;
%if &dsn IN ae ds co cm %then %do;
      Some---SAS—Statements;
%end;
%test;

Note:
No need of % sign in front of IN operator.
In SAS 9.2 both IN and # work if you use the system option minoperator inside your macro call.

%macro test/minoperator;
%if &dsn # ae ds co cm %then %do;
      Some---SAS—Statements;
%end;
%test;

%macro test;
%if &dsn IN ae ds co cm %then %do;
      Some---SAS—Statements;
%end;
%test;

MINOPERATOR option tells SAS to recognize the word 'IN' or special symbol # by the SAS macro facility as an infix operator when evaluating logical or integer expressions.

Here is another way of writing the macro code with delimiters.

Use MINDELIMITER option to change the default delimiter from space to any other, in this case it is comma (,).

options mindelimiter;

%macro test/mindelimiter=',';
%if &dsn IN ae,ds,co,cm %then %do;
      Some---SAS—Statements;
%end;
%test;

Sunday, September 6, 2009

CALL SYMPUTX / CALL SYMPUT

CALL SYMPUTX is similar to CALL SYMPUT except that

•CALL SYMPUTX does not write a note to the SAS log when the second argument is numeric. CALL SYMPUT, however, writes a note to the log stating that numeric values were converted to character values.

•CALL SYMPUTX uses a field width of up to 32 characters when it converts a numeric second argument to a character value. CALL SYMPUT uses a field width of up to 12 characters.

•CALL SYMPUTX left-justifies both arguments and trims trailing blanks. CALL SYMPUT does not left-justify the arguments, and trims trailing blanks from the first argument only. Leading blanks in the value of name cause an error.

•CALL SYMPUTX enables you to specify the symbol table in which to store the macro variable, whereas CALL SYMPUT does not. Symbol table is optional and the valid value of it is G, L, and F. If put G, then the macro variable will be stored in the global symbol table; else if specify L, SAS will store the macro in the local symbol table; else if not specify or specify F, SAS follows the same rules as like for Call Symput.

Thursday, September 3, 2009

Remove formats/informats/labels from a SAS dataset

data demo;
      set demo;
      attrib _all_ label=''; *remove labels;
      format _all_; *remove formats;
      informat _all_; *remove informats;
run;