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
- /
- General Programming
- /
- Summing with Arrays

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

01-16-2013 10:40 PM

Hello, I'm trying to run a simple code that will allow me to create a new variable (ppt_sum{i}) that will allow me to sum from variable i to variable 1. From the examples that I've found online, I should be able to write ppt_sum{i} = sum(ppt_lag{i-1}-ppt_lag1). However, my program interprets this to mean ppt_lag{i-1} *minus* ppt_lag1 (instead of *from* ppt_lag{i-1} *to* ppt_lag1). Any suggestions are greatly appreciated! Thanks!

Here is the full code that I have written:

data s1n_test;

set s1n_lags;

array ppt_lag{37};

array ppt_sum{37};

do i=1 to 37;

ppt_sum{i} = sum(ppt_lag{i-1}-ppt_lag1);

end;

run;

Accepted Solutions

Solution

01-17-2013
09:19 AM

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

01-17-2013 09:19 AM

Are you just trying to accomplish something like the following?:

data s1n_lags;

input ppt_lag1-ppt_lag37;

cards;

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

;

data s1n_test (drop=_;

set s1n_lags;

array ppt_lag{37};

array _ppt_lag(37);

array ppt_sum{37};

do i=1 to 37;

_ppt_lag(i)=ppt_lag(i);

ppt_sum{i} = sum(of _ppt_lag(*));

end;

run;

All Replies

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

01-16-2013 11:23 PM

Can you provide an example of what s1n_lags looks like? From your code I presume that it contains, at least, values for ppt_lag1 thru ppt_lag37. As such, besides the fact that it is written incorrectly, your sum statement doesn't make sense to me. Please explain.

Are you try to create 37 sums or just one?

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

Posted in reply to art297

01-17-2013 08:52 AM

Hi, thanks for the help.

I have a variable of precipitation (s2_precip_cm) and I'd like to create new variables that sum precipitation over various days. For instance, ppt_sum3 would be the sum of precipitation from day 1, the precipitation of the day prior, and the precipitation of two days prior. I was having trouble trying to write this in one code, so I've created a new dataset that lags precipitation by one day (s1n_lags). In this file, ppt_lag3 is the precipitation reading from three days prior to my observation. I was hoping that now I could sum across observations to obtain the correct variables. In this code, I was hoping that

ppt_sum{i} = sum(ppt_lag{i-1} - ppt_lag1)

would be equal (for i = 5) to

ppt_sum5 = sum(ppt_lag4, ppt_lag3, ppt_lag2, ppt_lag1)

and i = 6 would be equal to

ppt_sum6 = sum(ppt_lag5, ppt_lag4, ppt_lag3, ppt_lag2, ppt_lag1).

I'd like to do this for up to 37 observations.

Thank you so much for your help!

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

01-17-2013 09:09 AM

Are all 37 measures on the same record? And how many records do you have?

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

Posted in reply to art297

01-17-2013 09:19 AM

Yes, all 37 measures are on the same record and I have 18628 records.

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

01-17-2013 03:23 AM

Hi!

As far as I know, you can't sum a subset of array elements with one function call. You have to write a do-loop.

Mabye something like

data s1n_test;

set s1n_lags;

array ppt_lag{37};

array ppt_sum{37};

do i=1 to 37;

ppt_sum{i}=0;

do j=1 to i;

ppt_sum{i} = sum(ppt_sum{i},ppt_lag{j});

end;

end;

run;

Solution

01-17-2013
09:19 AM

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

01-17-2013 09:19 AM

Are you just trying to accomplish something like the following?:

data s1n_lags;

input ppt_lag1-ppt_lag37;

cards;

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

;

data s1n_test (drop=_;

set s1n_lags;

array ppt_lag{37};

array _ppt_lag(37);

array ppt_sum{37};

do i=1 to 37;

_ppt_lag(i)=ppt_lag(i);

ppt_sum{i} = sum(of _ppt_lag(*));

end;

run;

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

Posted in reply to art297

01-17-2013 09:35 AM

Arthur,

Yes, that's exactly what I'm trying to do. I was able to use your code to achieve my desired results. Thank you so much for your help!

Salli