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

id x

1 2

2 3

3 5

4 8

5 9

6 8

hi, i have sample dataset like above. i want to create a new variable y, such that,

if id = 1, then y = x;

else y(t)= y(t-1) * 2.

in this case:

y(1) = x(1) =  2

y(2) = y(1) *2 = 2*2 = 4

y(3) =y(2) *2 = 4*2 = 8

y(4) =y(3) *2 = 8*2 = 16

y(5) = y(4) *2 = 16*2 = 32

y(6) = y(5) *2 = 32*2 = 64

i am wondering if there is any function to achieve it? looks like lag can't do it.

thanks a lot for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Slash
Quartz | Level 8

Hi, you can try this method. You are right, LAG function can't do it.

data test;

input id x;

retain temp;

if id=1 then do;

  y=x;

  temp=y;

end;

if id ne 1 then do;

  y=temp*2;

  temp=y;

end;

drop temp;

cards;

1 2

2 3

3 5

4 8

5 9

6 8

;

run;

View solution in original post

5 REPLIES 5
Slash
Quartz | Level 8

Hi, you can try this method. You are right, LAG function can't do it.

data test;

input id x;

retain temp;

if id=1 then do;

  y=x;

  temp=y;

end;

if id ne 1 then do;

  y=temp*2;

  temp=y;

end;

drop temp;

cards;

1 2

2 3

3 5

4 8

5 9

6 8

;

run;

hadesmr
Calcite | Level 5

Smart! thank you!

Keith
Obsidian | Level 7

You can simplify the code a bit.

data test;

input id x;

retain y;

y=ifn(id=1,x,y*2);

cards;

1 2

2 3

3 5

4 8

5 9

6 8

;

run;

hadesmr
Calcite | Level 5

Thanks Keith, works better!

Slash
Quartz | Level 8

Year, this is easier.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 956 views
  • 3 likes
  • 3 in conversation