Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I perform a piece of macro for each observation in sas data step?

Tags:

sas

Suppose I allow user to write his own variable calculation macro using a common user interface:

%macro calculate(var_name, var_value);
%* Some user-defined calculation;
%mend calculate;

Then in a data step, I can calculate a new variable using the user-defined macro:

data dataset;
    set dataset;
    new_var = %calculate('variable1', variable1); * This doesn't work. It just shows my indication.
run;

Where variable1 is a variable in dataset. Here, I want to pass in the variable name and the actual value of the variable. After the calculation, put the value in new_var.

How can I achieve this?

like image 695
Steve Avatar asked Jun 09 '11 06:06

Steve


People also ask

How do you call a macro from a SAS DATA step?

Call Execute is a facility of the DATA step which allows executing SAS code generated by the DATA step. Also, the data from the DATA step can be used as part of the executable code in the Call Execute. The syntax of the Call Execute routine is rather simple: call execute('argument');

How do you create a macro variable in DATA step?

(Macro language statements always start with a %). This statement works much like an assignment statement in the DATA step. The %LET statement is followed by the macro variable name, an equal sign (=), and then the text value to be assigned to the macro variable. Notice that quotation marks are not used.

How do you create multiple macro variables in SAS?

Macro variables can be created from multiple columns in a single SELECT statement, by listing the source columns separated by a comma, and then listing the corresponding INTO :macro-variable statements separated by a comma. The macro variables are assigned in the order of the columns in the SELECT statement.


2 Answers

Is it required that you achieve this with macros? This sounds like a situation where PROC FCMP would be most useful as it allows you to define your own functions or subroutines (fcmp="function compiler") that can used in a data step just like a built-in function.

Here is a simple example:

proc fcmp outlib=sasuser.funcs.math;
  function calc(var);
     newvar=log(var); /*user defined stuff here - can be simple or complex*/
     return(newvar);
  endsub;
run;

option cmplib=sasuser.funcs; /*tell SAS where to look for functions*/
data _null_;
  set sashelp.class;
  newvar=calc(height); /*call your new function */
  put newvar=;
run;
like image 79
cmjohns Avatar answered Nov 16 '22 03:11

cmjohns


You can make this work, but you are probably writing the macro incorrectly. You have to remember that SAS macros are essentially text preprocessors: they input the code you have written and output code to feed to SAS [1].

So here is a simple "addition" macro:

%macro calculate (var_name, var_value);
  &var_name + &var_value;
%mend;

data one;
  input a@@;
  b = %calculate(a, 3);
  c = %calculate(a, a);
cards;
1 3 -2 4
;
run;

proc print data=one;
run;

The macro facility will replace the %calculate bits with the code generated by the macro, and SAS will actually see the following:

%macro calculate (var_name, var_value);
  &var_name + &var_value;
%mend;

data one;
  input a@@;
  b = a + 3;
  c = a + a;
cards;
1 3 -2 4
;
run;

proc print data=one;
run;

You can see it for yourself on the log file with the options mprint statement

Unfortunately, this will not cover all potential calculations. You might want to look up PROC FCMP for a way to generate custom functions/subroutines usable in the DATA step.

[1] I know it is more complicated than that, but you can get far thinking like this.

like image 26
Aniko Avatar answered Nov 16 '22 04:11

Aniko