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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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