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

I have to calculate the discount based on the purchased amount by the customer. For example if he has purchased for amount  1000 Rs, he should get 5% discount and if he bought for 2000 the discount should be 10% and if he has done for 3000 he should get 20% and above 3000 means he should get 30% discount.

 

So we have to calculate the  amount in the new variable  after the discount is applied .

 

cid  purchased_amt
  1  1000
  2  2000
  3  3000
  4  4000

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

PROC FORMAT is well suited for this kind of classification problem. See if you can use this as a template

 

proc format;
    invalue discount 
        0    -< 1000 = 0
        1000 -< 2000 = 0.05
        2000 -< 3000 = 0.2
        3000 - high = 0.3;
run;

data have;
input cid amt;
datalines;
1 1000
2 2000
3 3000
4 4000
;

data want;
    set have;
    dicount=input(amt, discount.);
run;

 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

PROC FORMAT is well suited for this kind of classification problem. See if you can use this as a template

 

proc format;
    invalue discount 
        0    -< 1000 = 0
        1000 -< 2000 = 0.05
        2000 -< 3000 = 0.2
        3000 - high = 0.3;
run;

data have;
input cid amt;
datalines;
1 1000
2 2000
3 3000
4 4000
;

data want;
    set have;
    dicount=input(amt, discount.);
run;

 

ed_sas_member
Meteorite | Level 14

Hi @rohithverma ,

 

You can try this. Hope this help!

data have;
	input cid amt;
	cards;
1 1000
2 2000
3 3000
4 4000
;
run;

data want;
    set have;
    format discount percent.;
    if 		   0 < amt <= 1000 then discount = 0;
    else if 1000 < amt <= 2000 then discount = 0.05;
    else if 2000 < amt <= 3000 then discount = 0.2;
    else discount = 0.3;
    new_amt = amt * (1-discount);
run;

 

 

Capture d’écran 2019-11-27 à 11.15.47.png

 

rohithverma
Obsidian | Level 7
Thank you very much..!
rohithverma
Obsidian | Level 7

Thank you very much..!

ballardw
Super User

@rohithverma wrote:

Thank you very much..!


One of the very powerful advantages to @PeterClemmensen's approach is that if the rules change for this discounting procedure you only have to make the change in the proc format code where it would be easy to modify the discount range, the actual discount or both.

A solution that involves If/then/else coding requires modifying more code with associated increased possibilities of typos or missing an end point for a range.

 

 

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
  • 2678 views
  • 5 likes
  • 4 in conversation