Help using Base SAS procedures

dividing 2 sum_values

Reply
N/A
Posts: 0

dividing 2 sum_values

Is is possible to divide 2 sum_values together accurately? I am aware that the sum values will carry over, and that is why so far I am not getting the correct answer, is there another function I can use to divide 2 sum_values?
Frequent Contributor
Posts: 102

Re: dividing 2 sum_values

Posted in reply to deleted_user
Please be more clear. Are you in a procedure, datastep, proc report?
N/A
Posts: 0

Re: dividing 2 sum_values

Posted in reply to CurtisMack
Here is the code I have written, detailing where I am running into problems,

/** combine two age groups and calculate the percentage of the poverty level with the same data set**/
data pov4;
set pov;
if age = 'under12' or age = 'under18' then sum_value + fpl100;
run;
proc print data = pov4;
title '5-18fpl100test';
run;
/**this is the part that the sum is coming up incorrect**/
data pov5;
set pov4;
if age = 'under12' or age = 'under18' then sum_value + total;
run;
proc print data = pov5;
title 'sumtotalof5-18poptest';
run;
data pov6;
set pov5;
percent = sum_value + fpl100 / sum_value + total * 100;
run;
proc print data = pov6;
title 'percentage of 5-17fpl100';
run;
Frequent Contributor
Posts: 102

Re: dividing 2 sum_values

Posted in reply to deleted_user
CGI,

Without seeing your data, I am not sure how to answer this. Honestly, you appear to need a class on SAS basics like the data step vector. I am going to show you two ways to do this. The first is similar to what you tried, the second is closer to how I would actually do it.


I am assuming your data looks something like this. However, I am sure it is much more complicated.

data pov;
length age $ 7;
age = 'under05'; total = 100; fpl100 = 20; output;
age = 'under12'; total = 300; fpl100 = 50; output;
age = 'under18'; total = 250; fpl100 = 60; output;
age = 'under40'; total = 1000; fpl100 = 100; output;
run;

Here is how I wouldn't do it because it assumes a lot, particularly that there are no values of age that will sort in between under12 and under18. I am showing it because it only uses the SAS techniques you were using.

proc sort data=pov;
by age;
run;

data pov5b(drop = fpl100 total) ;
set pov;
retain sum_fpl100 sum_total;
sum_fpl100 = sum(sum_fpl100,fpl100);
sum_total = sum(sum_total,total);
if age ne 'under12' then do;
if age = 'under18' then age = '5-17';
percent = sum_fpl100 / sum_total;
output;
sum_fpl100 = 0;
sum_total = 0;
end;
run;

This is a more robust way of doing it, but it requires that you understand SAS formats and proc summary. Note that if a format is passed a value that is it not written to handle, will return the value passed to it it. For this reason, only the affected age categories are listed.:

proc format;
value $newage
'under12' , 'under18' = '5-18pop';
run;

proc summary noprint nway data=pov;
var total fpl100;
class age;
format age $newage.;
output out = pov5 sum=;
run;

data pov6;
set pov5;
percent = fpl100 / total;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 121 views
  • 0 likes
  • 2 in conversation