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

Hi everyone, 

 

i try to reproduce the scenario below , any help / idea ?

 

YEARidxord WANTEXCEL Formula
110,0021812710,997818731-C2
210,0027853120,9950395E2*(1-C3)
310,0051955730,9898697E3*(1-C4)
310,0051955740,9898697E3*(1-C5)
410,0079117950,98203806E5*(1-C6)
410,0079117960,98203806E5*(1-C7)
120,0021812770,997818731-C8
220,0027853180,9950395E8*(1-C9)
320,0027853190,992268E9*(1-C10)
1 ACCEPTED SOLUTION

Accepted Solutions
bebess
Quartz | Level 8

thanks for your help , i added a line to the following code, in that case the count of _want restart to 1 for each new id in the dataset

 

your remarks is welcome 🙂

 


data want(drop=_:);
set have;
by id year;
if first.id then want=1-x;
if first.id then _want=1; /* to restart _want equal to 1 for each new id and not keep previsou value of the previous id*/
else want=_want*(1-x);
if last.year then _want=want;
retain _want;
run;

View solution in original post

6 REPLIES 6
bebess
Quartz | Level 8

i think something is missing because below what i have with your code :

 

in my case , i would like to keep same value from each same combination of year and id.

in the excel formula i always take previsous value of combiantion of year and id ( and not the last record )

 

example : year=3 and id=1  i expect to have same value  0,989869698 and not 0,984726761

 

yearidxwant
110,002181270,99781873
210,002785310,995039496
310,005195570,989869698
310,005195570,984726761
410,007911790,97693581
410,007911790,969206499
510,00963220,959870908
510,00963220,950625239
610,011991420,939225893
610,011991420,92796324
710,015670870,913421249
bebess
Quartz | Level 8

thanks for your help , i added a line to the following code, in that case the count of _want restart to 1 for each new id in the dataset

 

your remarks is welcome 🙂

 


data want(drop=_:);
set have;
by id year;
if first.id then want=1-x;
if first.id then _want=1; /* to restart _want equal to 1 for each new id and not keep previsou value of the previous id*/
else want=_want*(1-x);
if last.year then _want=want;
retain _want;
run;

Kurt_Bremser
Super User

Seems like you got it!

 

I would slightly change the layout of the code:

data want (drop=_:);
set have;
by id year;
retain _want;
if first.id
then do;
  want = 1 - x;
  _want = 1;
end;
else want = _want * (1 - x);
if last.year then _want = want;
run;

mainly to avoid the two IF FIRST.ID.

mkeintz
PROC Star

You can produce what you want by including SET/BY pair of statements inside a do until (last.id) loop:

 

data want;
  do until (last.id);
    set have;
	by id year;
	if first.year then _factor=coalesce(want,1);
	want=_factor*(1-x);
	output;
  end;
run;

Inside the loop, the _FACTORr variable is automatically retained, so it's easily updated with the advent of each new year.  And that updated value is taken from WANT (also retained inside the loop) prior to the recalculation of WANT.

 

The coalesce function is needed to establish a _FACTOR of 1 at the start of each id, since WANT is not retained across id's.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
bebess
Quartz | Level 8
thanks for sharing, will have a look on it !)

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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