I'm trying to take the difference of a variable and it's first value.
for a given variable:
var = 1 3 2 4 0 6
we get:
vardif = 0 2 1 3 -1 5
A related problem is to take the average of a few of the first values and subtract it from the variable:
var = 1 3 2 4 0 6
varavg = (1+3+2)/3=2
vardif2 = -1 1 0 2 -1 4
I tried the approach:
data have;
set have;
vardif = var1-FIRST..var1;
run;
without success.
Thanks for your help.
Here's an approach that addresses both questions:
data want;
if _n_=1 then do j=1 to 3;
set have;
total + var;
if j=1 then firstvar = var;
if j=3 then varavg = total / 3;
end;
retain varavg firstvar;
set have;
vardif = var - firstvar;
vardif2 = var - varavg;
drop j;
run;
Is there any requirement for grouping this on another variable. If so provide an example of the data set.
You can keep the value of a variable across records by using retain:
data want; /* habitually using same input and output for code will eventually create a condition where you don't know why some variable disappeared, has the wrong value and other oddnesses*/
set have;
retain firstvalue;
if _n_ = 1 then firstvalue=var;
run;
if you have a grouping variable then additional steps are needed.
You can get the mean of 3 values (current and previous 2) with:
varavg = mean(var, lag1(var),lag2(var));
any more provide example data in the form of a data step.
Here's an approach that addresses both questions:
data want;
if _n_=1 then do j=1 to 3;
set have;
total + var;
if j=1 then firstvar = var;
if j=3 then varavg = total / 3;
end;
retain varavg firstvar;
set have;
vardif = var - firstvar;
vardif2 = var - varavg;
drop j;
run;
@capam wrote:
I'm trying to take the difference of a variable and it's first value.
for a given variable:
var = 1 3 2 4 0 6
we get:
vardif = 0 2 1 3 -1 5
A related problem is to take the average of a few of the first values and subtract it from the variable:
var = 1 3 2 4 0 6
varavg = (1+3+2)/3=2
vardif2 = -1 1 0 2 -1 4
I tried the approach:
data have;
set have;
vardif = var1-FIRST..var1;
run;
without success.
Thanks for your help.
How exactly is your data structured?
Please post data as a data step ideally, but at least a more descriptive explanation would help. I can't tell if you have a single variable, or a single observation with multiple variables or a single variable with multiple observations.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.