BookmarkSubscribeRSS Feed
SASUser0001
Fluorite | Level 6

hi I have data as below I am looking to create another variable using %change column and number starting number from column. then use output of that and next % change value to create new variable and so on.  the new index column is what I wanted to create using starting value from B and then % from column A.

date         A      B        new_index

30-Sep-01   0.45% 3500        62799.435

31-Dec-02    1.02% 2000            3484.320557

31-Mar-04   0.58% 4800                3449.139336

30-Jun-05   0.93% 5698            3429.249688

30-Sep-06   0.74% 5896           3397.651529

 

 

data test_;
set Re_data;

do i= 1 to 5;

new_index=B/(1+per_change);

/*ouput of new_index then will become B and use next Per_change vallue*/


end;
run;
3 REPLIES 3
Tom
Super User Tom
Super User

I don't follow what you are trying to do.  Why does I go from 1 to 5?  What does I represent?

Is the value of NEW_INDEX represent what you are trying to create from B?  If so I cannot see how the math will work out.  62,799 is almost 18 times 3,500.

ballardw
Super User

First thing would be to provide an actual data step that shows the values that you currently have.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

 

Your code references an existing variable per_change. You do not actually show any existing value for "per_change". Do not name variables one thing and then show them with a different heading. If A is supposed to be per_change then the column heading should be Per_change or your code should reference A instead of per_change.

 

SAS will without additional code to change flow perform all of the code statements for each record in the data. So you do not need to attempt to "force" the five example rows to calculate with the Do loop if that is what you think you are attempting.

 

If you need the value calculated on one line of date to be kept for use on another then you use the RETAIN statement to indicate such use is intended for a variable. But you need to be a tad clearer about what you intend because it is not very clear at all.

 

 

FreelanceReinh
Jade | Level 19

Hi @SASUser0001,

 

You can use the implied loop of the DATA step instead of an explicit DO loop:

data have;
input date :date. A :percent. B;
format date date11. A percent7.2;
cards;
30-Sep-01 0.45% 3500
31-Dec-02 1.02% 2000
31-Mar-04 0.58% 4800
30-Jun-05 0.93% 5698
30-Sep-06 0.74% 5896
;

data want;
set have;
retain new_index 62799.435;
output;
if _n_=1 then new_index=b;
new_index=new_index/(1+a);
format new_index best11.;
run;

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