turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Arithmetic operations in sas

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-27-2014 05:14 PM

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_no | bolt_no | bolt_len | bolt_area | bolt_volume |

1 | 1.235344635 | |||

1 | 1 | 2 | 0.89358336 | 2.128927995 |

1 | 2 | 2 | 0.821784859 | 1.715368219 |

2 | 0.346877809 | |||

2 | 1 | 2 | 0.276875719 | 0.623753528 |

2 | 2 | 2 | 0.226900035 | 0.503775754 |

3 | 0.294638715 | |||

3 | 1 | 2 | 0.193085235 | 0.48772395 |

3 | 2 | 2 | 0.185057629 | 0.378142864 |

Thank you for your help.

Accepted Solutions

Solution

05-27-2014
05:34 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-27-2014 05:34 PM

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;

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-27-2014 05:30 PM

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;

Solution

05-27-2014
05:34 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-27-2014 05:34 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-28-2014 10:26 PM

Thanks **stat@sas **and **Arthur Tabachneck ,**

Both of your codes worked perfectly.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-28-2014 10:49 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 08:52 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 10:54 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-30-2014 03:28 PM

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

Regards,

Naeem

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-30-2014 03:54 PM

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

Thanks Arthur.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

2 weeks ago - last edited 2 weeks ago

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