BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sfd
Calcite | Level 5 sfd
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

7 REPLIES 7
art297
Opal | Level 21

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?

sfd
Calcite | Level 5 sfd
Calcite | Level 5

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!

art297
Opal | Level 21

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

sfd
Calcite | Level 5 sfd
Calcite | Level 5

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

Fraktalnisse
SAS Employee

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;

art297
Opal | Level 21

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;

sfd
Calcite | Level 5 sfd
Calcite | Level 5

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

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
  • 7 replies
  • 28783 views
  • 0 likes
  • 3 in conversation