Help using Base SAS procedures

creating new variables in sas

Reply
New Contributor
Posts: 2

creating new variables in sas

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

Frequent Contributor
Posts: 130

Re: creating new variables in sas

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.

New Contributor
Posts: 2

Re: creating new variables in sas

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"?

Super User
Posts: 17,819

Re: creating new variables in sas

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.

Frequent Contributor
Posts: 130

Re: creating new variables in sas

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!

Valued Guide
Posts: 858

Re: creating new variables in sas

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;

Super User
Posts: 10,497

Re: creating new variables in sas

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.

Ask a Question
Discussion stats
  • 6 replies
  • 364 views
  • 0 likes
  • 5 in conversation