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

I need to create two columns in a table, but the columns are dependant on each other. But there is no circular reference.  The table I need to create is similar to the example below.  The fields Account, Month, Original Balance and Perc already exist.  I need to calculate Cost and Running Balance.

The table is sorted by account and month.  For the first instance of an account appearing Cost is calculated as Original Balance * Perc and Running Bal is Original Balance-Cost.

In the subsequent rows Cost is the previous months Running Bal*Perc and the new Running Bal is the previous months Running Bal - this months Cost.

I've been able to create either the first row or the second row but not all rows for all accounts.

 

AccountMonthOriginal BalancePercCostRunning Bal
1234JAN10001.0%10990.00
1234FEB10001.2%11.9978.12
1234MAR10001.1%10.8967.36
1234APR10001.3%12.6954.78
1234MAY10001.0%9.5945.24
1234JUN10000.9%8.5936.73
4561JAN12000.9%10.681189.32
4561FEB12000.8%9.81179.57
4561MAR12000.8%8.81170.72
4561APR12000.7%8.01162.76
4561MAY12000.6%7.11155.67
4561JUN12000.5%6.21149.43
1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @CStanyer76 

 

Here is another method.

In my opinion, you should not round values during the calculation process, but rather use a format for display.

 

data have;
	infile datalines dlm="09"x;
	input Account Month $ Original_Balance Perc:percent3.1;
	format Perc percent8.1;
	datalines;
1234	JAN	1000	1.0%
1234	FEB	1000	1.2%
1234	MAR	1000	1.1%
1234	APR	1000	1.3%
1234	MAY	1000	1.0%
1234	JUN	1000	0.9%
4561	JAN	1200	0.9%
4561	FEB	1200	0.8%
4561	MAR	1200	0.8%
4561	APR	1200	0.7%
4561	MAY	1200	0.6%
4561	JUN	1200	0.5%
;
run;

data want;
	set have;
	
	by Account Month notsorted;
	
	format Cost Running_Bal 10.2;
	
	retain Cost;
	retain Running_Bal;
	
	if first.Account then do;
		Cost = Original_Balance * Perc;
		Running_Bal = Original_Balance-Cost;
	end;
	
	else do;
		Cost = Running_Bal * Perc;
		Running_Bal = Running_Bal - Cost;
	end;

run;

 

View solution in original post

4 REPLIES 4
Satish_Parida
Lapis Lazuli | Level 10
data have;
input Account $	Month $	Original_Balance Perc;
cards;
1234 JAN 1000 1.0
1234 FEB 1000 1.2
1234 MAR 1000 1.1
1234 APR 1000 1.3
1234 MAY 1000 1.0
1234 JUN 1000 0.9
4561 JAN 1200 0.9
4561 FEB 1200 0.8
4561 MAR 1200 0.8
4561 APR 1200 0.7
4561 MAY 1200 0.6
4561 JUN 1200 0.5
;
run;

data want(drop=current);
length current 8.;
set have;
by Account;
retain current;
if first.Account then do;
	Cost=round(Original_Balance*Perc/100,.01);
	Running_Bal=Original_Balance-Cost;
	current=Running_Bal;
end;
else do;
	Cost=round(current*Perc/100,.01);
	Running_Bal=current-Cost;
	current=Running_Bal;
end;
run;

Suggestion : Use months as numbers, which will be easy to sort.

Please let us know if this worked for you.

 

@Experts, Can we do it using Lag Function?

CStanyer76
Calcite | Level 5

This works, thank you!

 

ed_sas_member
Meteorite | Level 14

Hi @CStanyer76 

 

Here is another method.

In my opinion, you should not round values during the calculation process, but rather use a format for display.

 

data have;
	infile datalines dlm="09"x;
	input Account Month $ Original_Balance Perc:percent3.1;
	format Perc percent8.1;
	datalines;
1234	JAN	1000	1.0%
1234	FEB	1000	1.2%
1234	MAR	1000	1.1%
1234	APR	1000	1.3%
1234	MAY	1000	1.0%
1234	JUN	1000	0.9%
4561	JAN	1200	0.9%
4561	FEB	1200	0.8%
4561	MAR	1200	0.8%
4561	APR	1200	0.7%
4561	MAY	1200	0.6%
4561	JUN	1200	0.5%
;
run;

data want;
	set have;
	
	by Account Month notsorted;
	
	format Cost Running_Bal 10.2;
	
	retain Cost;
	retain Running_Bal;
	
	if first.Account then do;
		Cost = Original_Balance * Perc;
		Running_Bal = Original_Balance-Cost;
	end;
	
	else do;
		Cost = Running_Bal * Perc;
		Running_Bal = Running_Bal - Cost;
	end;

run;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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