I want to create a new variable which is equal to the column sum of an existing variable monthly_sales.the code i am using is-
data west;
input total;
set yash123;
total=sum monthly_sales;
proc print data=west;
var total;
run;
but it throws an error stating variable total not found.Please help
You shouldn't need an input statement for your total variable you're creating. You can use a format statement if you wish to define the variable a format ahead of time. I think your issue is how you're trying to calculate the variable total. Are you trying to sum all monthly_sales? You should be getting a syntax error with your "total=sum monthly_sales;" statement.
If you're looking to sum all monthly_sales observations to get a total amount, I would use a proc means statement or a proc sql with a group by statement. Unless you're trying to cacluate total a different way.
Hope that helps.
hi ,
Thanks for your reply. proc means and proc sql can be used to calculate sum.But then how can we relate that value to the new variable "total"?
yashchawla wrote:
But then how can we relate that value to the new variable "total"?
What does that mean? A picture is worth a 1000 words, please post input and output data.
If you mean how to create a variable named total that equals the total sum, then try this:
proc means data=yash123 sum nway noprint;
var monthly_sales;
output out=west (drop=_TYPE_ _FREQ_)
sum=Total;
run;
This will give you a data set called west with a variable called Total that is equal to the sum of all monthly_sales. If you want to see the sum of monthly_sales by Month, like Mark Johnson laid out, add a class statement to the means procedure with Month as your class:
proc means data=yash123 sum nway noprint;
var monthly_sales;
class month;
output out=west (drop=_TYPE_ _FREQ_)
sum=Total;
run;
Hope that helps!
It would be easier if you gave an example of the data you have and want you want. Here's a guess at your solution though:
data have;
input month monthly_sales;
cards;
1 100
1 200
2 50
2 100
3 25
3 1
4 0
4 100
5 5000
5 100
6 100
6 1
;
run;
proc sql;
create table want as
select *,sum(monthly_sales) as sum
from have
group by month;
My $0.02: In my opinion about the only reason to have a column sum (or other summary) is for a report as data like that is very dangerous to have in a data set used for analysis. So use of a reporting procedure and generate the report directly is preferable.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.