SaS Code help

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

SaS Code help

So I am trying to write a code that will sum the prices for a certain id

So the data I have is

ID Name Price

1   s       10

1   s       20

1   s       10

2   a       20

2   a       20

3   d       30

3   d       30

3   d       30

then I want through the code to give me

1 s 40

2 a 40

3 d 90

any suggestions?


Accepted Solutions
Solution
‎10-13-2012 08:22 AM
Super User
Super User
Posts: 7,039

Re: SaS Code help

Looks like you are using Enterprise Guide.  There is probably a GUI tool to generate sum.

Here is code using PROC MEANS.

If you just want a report then use:


proc means data=HAVE sum ;

  by id first_name last_name;

  var price;

run;

If you want to generate a data set you can add the OUTPUT statement.  You can also add NOPRINT option to the PROC statement (or switch from proc means to proc summary).


proc means noprint data=have ;

  by id first_name last_name;

  var price;

  output out=want sum=total;

run;

View solution in original post


All Replies
Super User
Posts: 19,772

Re: SaS Code help

Proc means, proc univariate, proc summary...so many options.

Try proc means.

proc means data=sashelp.class sum;

class sex age;

var age;

run;

New Contributor
Posts: 3

Re: SaS Code help

I will post pictures, since I was given what the end result should look like but I have no idea how to get his end result

Capture.JPGphoto.JPG

Solution
‎10-13-2012 08:22 AM
Super User
Super User
Posts: 7,039

Re: SaS Code help

Looks like you are using Enterprise Guide.  There is probably a GUI tool to generate sum.

Here is code using PROC MEANS.

If you just want a report then use:


proc means data=HAVE sum ;

  by id first_name last_name;

  var price;

run;

If you want to generate a data set you can add the OUTPUT statement.  You can also add NOPRINT option to the PROC statement (or switch from proc means to proc summary).


proc means noprint data=have ;

  by id first_name last_name;

  var price;

  output out=want sum=total;

run;

PROC Star
Posts: 1,167

Re: SaS Code help

And it's a handoff from Tom to Tom!


To get code very similar to Tom's in Enterprise Guide:


1. Have your dataset open, as in your screen shot.

2. Select Describe | Summary Statistics Wizard.

3. Click Next on the first screen.

4. On the "Assign Variables to Roles" screen, drag ID and Name into Classification Variable, and Price into Analysis Variable. Click Next.

5. On the "Specify Statistics and Results" screen, click Edit, and rearrange the statistics so that only Sum is selected. Click OK to close the Edit screen.

6. Click Next and Finish, and you should have your report.


If you want a results dataset instead of a report:


At step 5.

5a. After you have set the statistics to Sum and returned to the "Specify Statistics and Results" screen, untick "Show Statistics" and tick "Save statistics to dataset". Then when you click Next and Finish, you'll get a SAS dataset with the results (ignore the variables that start with _; you'll learn about them as you use SAS more).

Tom


New Contributor
Posts: 3

Re: SaS Code help

thank you so much tom! It was annoying me that I knew sort of what do but it wasn't working! Thank you so much again!!

Occasional Contributor
Posts: 9

Re: SaS Code help

may use:

proc sql; create table new_dataset as select distinct id, name, sum(price) as sum_prc

from original_dataset group by id; quit;

New Contributor
Posts: 2

Re: SaS Code help

Using the BY group processing and will get desired output

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1067 views
  • 6 likes
  • 6 in conversation