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
- /
- SAS Programming
- /
- SAS Procedures
- /
- How to sum across

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-04-2017 03:39 PM

How i need to sum first column with another column across?

i mean this:

```
data stock;
input Astock Bstock;
cards;
500 250
200 658
300 425
400 125
;
run;
data sum;
set stock;
sum=sum(Astock,Bstock);
run;
```

How i need to change it ?

Accepted Solutions

Solution

05-04-2017
03:54 PM

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

Posted in reply to Sizzen

05-04-2017 03:44 PM

Like this?

```
data stock;
input Astock Bstock;
cards;
500 250
200 658
300 425
400 125
;
run;
data want;
set stock;
lag_Astock = lag1(Astock);
if not missing(lag_Astock) then total = sum(Bstock,lag_Astock);
drop lag_Astock;
run;
```

All Replies

Solution

05-04-2017
03:54 PM

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

Posted in reply to Sizzen

05-04-2017 03:44 PM

Like this?

```
data stock;
input Astock Bstock;
cards;
500 250
200 658
300 425
400 125
;
run;
data want;
set stock;
lag_Astock = lag1(Astock);
if not missing(lag_Astock) then total = sum(Bstock,lag_Astock);
drop lag_Astock;
run;
```

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

Posted in reply to draycut

05-04-2017 03:54 PM

Thanks !!

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

Posted in reply to draycut

05-04-2017 04:16 PM

One more question :

If i have

And i need to find average of three variables and write to next, but first meaning is missing, so i don't need that;;

```
data numbers;
input A;
cards;
500
400
300
200
800
200
;
run;
data mean;
set numbers;
first = lag(A);
second=lag2(A);
third=lag3(A);
if _n_ ge 3 then mean= mean(of first,second,third);
final=lag1(mean);
run;
```

but first of all it find mean of missing value+500+400/3=300

How to change ?

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

Posted in reply to Sizzen

05-04-2017 06:33 PM

If you want a value to be counted in the denominator (3) then there must be a value in the numerator. which would have to be 0 if I understand what you are attempting if you want to use the MEAN function.

Or

mean = sum(of first,second,third) / 3;

When you start getting to complicated requirements to use values that are not in your data then you will have to provide the values using your business logic.