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

I've the data as follows and I need to calculate the percentage of difference between the variables 'amount1' and 'amount2' by ignoring the negative sign.

 

Input data:

 

AMOUNT1 AMOUNT2
100 110
200 220
1000 100
100 20
1000 900

 

Desired Output:

 

AMOUNT1 AMOUNT2 Diff in percent
100 110 10
200 220 10
1000 100 90
100 20 80
1000 900 10

 

Datastep to create the data:

 

data A;
Input AMOUNT1 AMOUNT2 ;
cards;
100 110
200 220
1000 100
100 20
1000 900
;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

If you want the differences shown with a percent sign, the easiest is to calculate the absolute relative difference, and use the PERCENT. format to display as a percent:

data want;
  set a;
  rel_diff=abs((amount1-amount2)/amount1);
  format rel_diff percent5.0;
run;

View solution in original post

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12
Use range( amount1, amount2) / amount1
trungdungtran
Obsidian | Level 7

You can use

 

data B;
	set A;
	diff=round(abs(AMOUNT2-AMOUNT1)/AMOUNT1*100);
run;

where abs is absolute value, i.e., ignoring negative. If you do not need round, then you can remove it.

Babloo
Rhodochrosite | Level 12

What format should I use to display the percentage with percent sign (%) as shown below?

 

Diff in percent
10%
10%
90%
80%
10%
s_lassen
Meteorite | Level 14

If you want the differences shown with a percent sign, the easiest is to calculate the absolute relative difference, and use the PERCENT. format to display as a percent:

data want;
  set a;
  rel_diff=abs((amount1-amount2)/amount1);
  format rel_diff percent5.0;
run;

Kurt_Bremser
Super User

@Babloo wrote:

What format should I use to display the percentage with percent sign (%) as shown below?

 

Diff in percent
10%
10%
90%
80%
10%

Are you really too lazy to google "sas percent"?

trungdungtran
Obsidian | Level 7
I agree with you that it seems to lazy. I had this feeling from the first post and now you share the same feeling.
FreelanceReinh
Jade | Level 19

@Babloo wrote:

 

Diff in percent
10%


Also, I don't think it's the best style to write both percent signs after the numbers and "in percent" in the header (same with other units of measurement).

FreelanceReinh
Jade | Level 19

Or try this:

data want;
set a;
if amount1>0 then diffpct=round(abs(amount2/amount1-1)*100,1e-9);
else diffpct=.;
label diffpct='Diff in percent';
run;

proc print data=want label noobs;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 9941 views
  • 2 likes
  • 6 in conversation