BookmarkSubscribeRSS Feed
eemrun
Obsidian | Level 7

Simple data manipulation problemthat I just cannot get past! I have a dataset with the following variables:

 

A B C Iter

1 2 9   1

2 5 7   1

3 9 6   1

4 1 3   2

 

I also have another dataset with just one row of info. 

 

A_A B_B C_C

1      3      7

 

what i need to do is the following:

 

For ITER = 1

 

First A_A_P1 = A_A + A  -> 1 + 1 = 2

 

For subsequent ITER = 1

 

A_A_P1 = lag(A_A_P1) + A -> 2 + 2 = 4

 

So the A_A_P1 column should look like this:

 

A_A_P1

2

4

7

11

 

For iter = 2, same process but now the values will be under  A_A_P2. 

 

The same process will continue for B and C with respective ITERs.

 

Any idea how this can be done efficiently?

3 REPLIES 3
Reeza
Super User

1. What would be the output for the sample data you've posted?

2. What code do you have so far?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Start by re-modelling your data so that it is in a simple usable structure, something like:

ITER   CLASS   VALUE  ROWID
1          A            1           1
1          B            2           1
1          C            9           1
1          A            2            2
...

CLASS   VALUE
A             1
B             3
C             7

With this model you can then simply merge the second dataset onto the first one and do your comuptations.

Ksharp
Super User

Here is how to get A_A_P1. But I don't understand your next few questions.

 

data have;
input A B C Iter;
cards;
1 2 9   1
2 5 7   1
3 9 6   1
4 1 3   2
;
run;
data x;
input A_A B_B C_C;
cards;
1      3      7
;
run;
data want;
 set have;
 if _n_=1 then set x;
 retain A_A_P1;
 if _n_=1 then A_A_P1=A+A_A;
  else A_A_P1=A+A_A_P1;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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