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

I have a data set with variable a. I need to create variable b that is equal in line 1 to (1+a) and in each of the following lines to the value created for b in the previous line multiplied by (1+a) (a of the current line). I tried different things and each time run into another problem. The problem is how ot get the lag of variable b I just computed in the previous line. I tried using retain, and computing a lag variable outside the if statement, and it still does not work. For example:

data test2;

set test;

retain b;

lagb = lag(b);

if _N_ = 1 then b = sum(1,a);

else b= lagb *  sum(1,a);

run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Depending on your objective here, it is possible that a very simple program will do. Try:

data test2;
set test;
retain b 1;
b = b * sum(1, a);
run;

If that gives you the right result, great. If not, you can still get the lag you are asking for. Use:

data test2:
lagb=b;
set test;
retain b;
if _n_=1 then b=sum(1, a);
else b=lagb * sum(1, a);
run;

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

Please provide example data and expected output for that data.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

To continue with that approach you would need two datasteps, one to calculate the variable, the second to do the lag.  You cannot use lag() and variables which are not fixed.  There are other ways, such as retain, and merging on obs=obs+1.  However without test data in the form of a datastep we cannot provide anything.

Taliah
Obsidian | Level 7

Thank you for your replys. I simplified the data. input data:

a

10

2

4

8

5

 

required output:

a   b

10  11

2    33

4   165

8  1485

5  8910

 

 As explained above, b is equal in line 1 to (1+a) and in each of the following lines to the value created for b in the previous line multiplied by (1+a) (a of the current line).

Astounding
PROC Star
Depending on your objective here, it is possible that a very simple program will do. Try:

data test2;
set test;
retain b 1;
b = b * sum(1, a);
run;

If that gives you the right result, great. If not, you can still get the lag you are asking for. Use:

data test2:
lagb=b;
set test;
retain b;
if _n_=1 then b=sum(1, a);
else b=lagb * sum(1, a);
run;
Taliah
Obsidian | Level 7

Thank you! Looks like the first code you wrote does the job.

I see what you did with the retain b and using that instead of lag.

Does the retaon 1 retain line 1?

Thank you again!

Astounding
PROC Star

This statement:

 

retain b 1;

 

both retains B and gives it an initial value of 1  "Initial value" means at the very beginning of the DATA step, before reading in any of the data.  It does NOT mean at the beginning of each observation.

Taliah
Obsidian | Level 7

Thank you for all your help!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 901 views
  • 0 likes
  • 4 in conversation