2
votes

Assume that I have a SAS data step, where I subtract every observation (say, I only have variable X) from its mean:

data tmp;
  set tmp;
  x = x-2;
run;

Let's say mean is not always 2 and I have another script that creates a text file with one line, which contains:

x = x-2;

Now, the question is, is there any way I can have something like:

data tmp;
  set tmp;
  load text_file;
run;

To do the same thing as the first data step? In other words, I want a solution that relies on using the content of the file (either as I showed in the data step or within a macro).

1

1 Answers

5
votes

%INCLUDE will do what you want. Assuming your text file "c:\mycode.sas" has the line

x=x-2;

then you can do this:

data tmp;
set tmp;
%include "c:\mycode.sas";
run;

I'd note that this is a really, really bad way to do this, but it's what you asked for.

If I wanted to subtract the mean of x from x (standardizing the data), I'd either use PROC STDIZE, or do this:

proc means data=tmp;
var x;
output out=x_mean mean=x_bar;
run;

data want;
set tmp;
if _n_ = 1 then set x_mean;
x=x-x_bar;
run;

Or, PROC STDIZE (included in SAS/STAT):

proc stdize data=tmp out=want_std method=mean;
var x;
run;