SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
capam
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

 

View solution in original post

3 REPLIES 3
ballardw
Super User

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.

Astounding
PROC Star

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;

 

Reeza
Super User

@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.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2306 views
  • 0 likes
  • 4 in conversation