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

Dear users,

 

i would like to replicate the result ( column want ) of my Excel example below .

 

any help on it i tried many ways , maybe the code below is close to get the right results.

 

 
agecountryregionxwant
1FREurope0,51
2FREurope0,60,5
3FREurope0,70,2
4FREurope0,80,06
5FREurope0,90,012
1USAmerica0,11
2USAmerica0,20,9
3USAmerica0,30,72
4USAmerica0,40,504
5USAmerica0,50,3024

 

 

data want;
set have;
by  country region age;
if first.country then y=1;
else y=lag(y)*(1-lag(x));
run;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why are you calling LAG(Y)?  That neither makes any sense if Y is not one of your source variables nor if you want to use the retained value in the calculation.

data want;
  set have;
  by country region age;
  y=y*(1-lag(x));
  if first.country then y=1;
  retain y;
run;

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

@bebess wrote:

...

data want;
set have;
by  country region age;
if first.country then y=1;
else y=lag(y)*(1-lag(x));
run;

 

That is not going to work right.  By not calling the LAG() function for the first value of each COUNTRY the values of X and Y from that observation never make it into the list of values it has available to return.  

Do this instead so that LAG() runs on every observation.

y=lag(y)*(1-lag(x));
if first.country then y=1;

 

bebess
Quartz | Level 8

thanks for your help ,

the proposed code give me the following result 

 

agecountryregionxwanty
1FREurope0,511
2FREurope0,60,5 
3FREurope0,70,2 
4FREurope0,80,06 
5FREurope0,90,012 
1USAmerica0,111
2USAmerica0,20,9 
3USAmerica0,30,72 
4USAmerica0,40,504 
5USAmerica0,50,3024 

 

data want;
set have;
by  country region age;
y=lag(y)*(1-lag(x));
if first.country then y=1;
run;

when i tried retain as below i've got wrong result comparing to column want

 

agecountryregionxwanty
1FREurope0,511
2FREurope0,60,50,5
3FREurope0,70,20,4
4FREurope0,80,060,15
5FREurope0,90,0120,08
1USAmerica0,111
2USAmerica0,20,90,072
3USAmerica0,30,720,8
4USAmerica0,40,5040,0504
5USAmerica0,50,30240,48

 

data want;
set have;
by  country region age;
retain y 1;
y=lag(y)*(1-lag(x));
if first.country then y=1;
run;
Tom
Super User Tom
Super User

Why are you calling LAG(Y)?  That neither makes any sense if Y is not one of your source variables nor if you want to use the retained value in the calculation.

data want;
  set have;
  by country region age;
  y=y*(1-lag(x));
  if first.country then y=1;
  retain y;
run;

 

bebess
Quartz | Level 8
Many thanks , i was a little confused , this is exactly what i wanted ...
qatman28
Obsidian | Level 7

 

data have;
input age country $ region $ x;
datalines;
1 FR Europe 0.5
2 FR Europe 0.6
3 FR Europe 0.7
4 FR Europe 0.8
5 FR Europe 0.9
1 US America 0.1
2 US America 0.2
3 US America 0.3
4 US America 0.4
5 US America 0.5
;

proc sort data=have;
   by country region age;

data want (drop=prev_y prev_x);
   set have;
   by country region;
   retain prev_x prev_y;
   if first.region then y = 1;
   else y = prev_y * (1 - prev_x);
   prev_y = y;
   prev_x = x;
run;

proc print data=want noobs;

** Results **;
age	country	region	x	y
1	FR	Europe	0.5	1.0000
2	FR	Europe	0.6	0.5000
3	FR	Europe	0.7	0.2000
4	FR	Europe	0.8	0.0600
5	FR	Europe	0.9	0.0120
1	US	America	0.1	1.0000
2	US	America	0.2	0.9000
3	US	America	0.3	0.7200
4	US	America	0.4	0.5040
5	US	America	0.5	0.3024

This code explicitly uses retain to keep the previous values.  I find it simpler to know exactly which variables are being kept.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1569 views
  • 2 likes
  • 3 in conversation