## find the difference in a variable

Solved
Frequent Contributor
Posts: 77

# find the difference in a variable

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.

Accepted Solutions
Solution
‎01-12-2018 09:38 AM
Super User
Posts: 6,624

## Re: find the difference in a variable

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;

All Replies
Super User
Posts: 13,292

## Re: find the difference in a variable

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.

Solution
‎01-12-2018 09:38 AM
Super User
Posts: 6,624

## Re: find the difference in a variable

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;

Super User
Posts: 23,228

## Re: find the difference in a variable

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.

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.

☑ This topic is solved.