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

so here's is my coding. from my data the first totalprice*unit =70MYR. when I run it, the discount I get is 4.90MYR. If is calculate it manually, the discount that I should get is 3.50MYR. Does anyone know how to do it correctly?

 

DATA Payment;
SET combined;
LENGTH total $ 10;
LABEL total='Total Price (RM)'
after='Total Price after Discount (RM)'
dis='Discount (RM)'
fee='Courier Fee (RM)'
net='Net Amount (RM)';
total='Price (RM)'n*unit;
IF total>=20 THEN after=total-dis;
ELSE dis=total*0.05;
IF total>=30 THEN dis=total*0.07;
ELSE dis=total*0.00;
after=total-dis;
net=after+5;
RUN;
PROC PRINT DATA=Payment label;
TITLE 'Monarch Online Shop';
FOOTNOTE 'STAY AT HOME AND STAY SAFE';
RUN;

 

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

Think through your conditional statements using 70MYR. Are they what you intend?

 

From your logic, total = 70 goes into the first IF statement (since 70 is >= 20), so AFTER is set to total-dis. And total =70 also goes into the second IF statement (since 70 is >= 30), so DIS is set to 70 * 0.07. So from your logic, 4.90MYR is the correct result. 

 

Is DIS = TOTAL * 0.05 supposed to be applied when Total is >= 20? Right now, since you have that in an ELSE statement, it won't be applied.

View solution in original post

8 REPLIES 8
mklangley
Lapis Lazuli | Level 10

Think through your conditional statements using 70MYR. Are they what you intend?

 

From your logic, total = 70 goes into the first IF statement (since 70 is >= 20), so AFTER is set to total-dis. And total =70 also goes into the second IF statement (since 70 is >= 30), so DIS is set to 70 * 0.07. So from your logic, 4.90MYR is the correct result. 

 

Is DIS = TOTAL * 0.05 supposed to be applied when Total is >= 20? Right now, since you have that in an ELSE statement, it won't be applied.

IdlanHnf
Obsidian | Level 7

Buyer that spent more than RM20 (excluding courier fee) will receive 5% discount, and buyer that spent more than RM30 (excluding courier fee) will receive 7% discount. Using SAS, calculate the net amount that the buyer needs to pay for that order (cost of item + courier fee). This is the question I got.

 

How can I make that if total=70, it only use either one of the statement?

mklangley
Lapis Lazuli | Level 10

@IdlanHnf Try this:

IF total>=30
THEN dis=total*0.07;
ELSE IF total>=20
THEN dis=total*0.05;
ELSE dis=total*0.00;

after = total-dis;

 

IdlanHnf
Obsidian | Level 7

thank you for your help. I understand now what was my mistake. thanks for enlightening me.

IdlanHnf
Obsidian | Level 7

as you can see at Label, I put fee='Courier Fee (RM)'. how can I add another line of coding that consist of detail about the fee? for example, I have 2 couriers, PosLaju, and GDex with a different fee, 2MYR, and 3MYR, respectively. and how to add that into a function like this? i want to make it become if my first order is using PosLaju, so for the function below, fee become 2MYR

net=after+fee;

 

mklangley
Lapis Lazuli | Level 10

@IdlanHnf Since this discussion is closed ("Solved"), if you have a follow-up question, it would be best to post it as a new discussion (including your question, your code, etc.). You'll get a lot more people viewing it then.

ballardw
Super User

Look closely here:

	IF 		total>=20 THEN after=total-dis;
	ELSE 	dis=total*0.05;
	IF 		total>=30 THEN dis=total*0.07;
	ELSE 	dis=total*0.00;
after=total-dis;
net=after+5;

So when you have Total>= 20 you calculate After twice with different values of dis. So, which one do you expect? Also the first time you use total-dis the value of dis is not set, so that After is missing as a result.

 

You also need to consider that every value >30 is also greater than 20. Perhaps sequence of calculation is not as you do manually. Computers don't know to stop unless you tell them to explicitly.

I suggest that every where you have a "dis =" to create a different variable: dis1 = <calculation>; dis2= <calculation> . Then look at the different values and see what is going on.

Reeza
Super User
Your criteria is not mutually exclusive which is weird. If your total is greater than 30, it passes both criteria.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 963 views
  • 2 likes
  • 4 in conversation