This widget could not be displayed.
This widget could not be displayed.
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Hi everyone.

I have a data set and I attached it to my message.  You can see it below.

I want to calculate bolt_volume. It looks simple but I couldn't get correct results. The formula has already written in excel sheet.

bolt_volume[row2] = ( bolt_area[row1] + bolt_area[row2] ) / 2 * bolt_len[row2]

bolt_volume[row3] = ( bolt_area[row2] + bolt_area[row3] ) / 2 * bolt_len[row3]

...........

tree_nobolt_nobolt_lenbolt_areabolt_volume
11.235344635
1120.893583362.128927995
1220.8217848591.715368219
20.346877809
2120.2768757190.623753528
2220.2269000350.503775754
30.294638715
3120.1930852350.48772395
3220.1850576290.378142864

Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Here is one way:

data want;

  set have;

  by tree_no;

  bolt_volume=ifn(first.tree_no,.,lag(bolt_area)+bolt_area) / 2 * bolt_len;

run;

View solution in original post

9 REPLIES 9
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Hi,

This should work for the desired output.

Thanks,

Naeem

data want;

set have;

bolt_volume=(bolt_area+lag(bolt_area))/2*bolt_len;

run;

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Here is one way:

data want;

  set have;

  by tree_no;

  bolt_volume=ifn(first.tree_no,.,lag(bolt_area)+bolt_area) / 2 * bolt_len;

run;

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Thanks

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Just be careful with Naeem's code as it will give you non-missing, and wrong values, for the 1st record of the 2nd thru Nth by groups.

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

@,

My original data set has more than 2000 observations. After I saw your message, I checked your codes and Nadeem's codes for my data set. But I couldn't see wrong values 1st record of the 2nd through Nth by groups.

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

I stand corrected!  I hadn't noticed that bolt_len was always missing for your first record within an id.  If that is the case in all situations, then Naeem's code will provide the correct result.

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Thanks Arthur - Yes, if first id is not missing then your suggested solution is a definite choice.

Regards,

Naeem

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Ok. I see now. That's tender spot.

Thanks Arthur.

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

An operator in SAS is a symbol which is used in a mathematical, logical or comparison expression.

 

Example Arithmetic Operators.

 

DATA MYDATA1;
input @1 COL1 4.2 @7 COL2 3.1;
Add_result = COL1+COL2;
Sub_result = COL1-COL2;
Mult_result = COL1*COL2;
Div_result = COL1/COL2;
Expo_result = COL1**COL2;
datalines;
11.21 5.3
3.11 11
;
PROC PRINT DATA=MYDATA1;
RUN;

 

On running the above code, we get the following output.

 

operators_arithmrtic.jpg

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 5492 views
  • 4 likes
  • 4 in conversation
This widget could not be displayed.
This widget could not be displayed.