BookmarkSubscribeRSS Feed
elopomorph88
Calcite | Level 5

I have a dataset that is similar to: 

 

Person           boat             Fish           

John                A                  2

David               A                  1

Sam                 B                  4

Mike                 C                  3

Carley              C                  5

Mark                 C                  1

 

And I want to add a column to my data that has the total number of fish per boat.  The boats are A, B, and C.  However, I just want to add the column to my data and don't want to reorganize the rest of my dataset.  I tried proc summary but it kept reorganizing my dataset.  Maybe I am doing it wrong.  I want my new dataset to have a column for FishperBoat and to look like:  

 

Person           boat             Fish         FishperBoat          

John                A                  2                    3

David               A                  1                    3

Sam                 B                  4                   4

Mike                 C                  3                   9

Carley              C                  5                   9

Mark                 C                  1                   9

 

Any advice would be greatly appreciated.  

3 REPLIES 3
Astounding
PROC Star

You started out well by using PROC SUMMARY.  That gives you the total fish per boat, in a separate data set (at least it does if you ran the proper PROC SUMMARY).  As a final step, you need to merge the output data set from PROC SUMMARY with your original data set.

 

See how far you get with that idea, and post your log if you have any difficulties.

Reeza
Super User

I outline a couple of different ways in this post. You should be able to run the code entirely from the site to check it out and understand it.

 

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

 


@elopomorph88 wrote:

I have a dataset that is similar to: 

 

Person           boat             Fish           

John                A                  2

David               A                  1

Sam                 B                  4

Mike                 C                  3

Carley              C                  5

Mark                 C                  1

 

And I want to add a column to my data that has the total number of fish per boat.  The boats are A, B, and C.  However, I just want to add the column to my data and don't want to reorganize the rest of my dataset.  I tried proc summary but it kept reorganizing my dataset.  Maybe I am doing it wrong.  I want my new dataset to have a column for FishperBoat and to look like:  

 

Person           boat             Fish         FishperBoat          

John                A                  2                    3

David               A                  1                    3

Sam                 B                  4                   4

Mike                 C                  3                   9

Carley              C                  5                   9

Mark                 C                  1                   9

 

Any advice would be greatly appreciated.  


 

Ksharp
Super User
data have;
input Person     $      boat     $        Fish  ;         
cards;
John                A                  2
David               A                  1
Sam                 B                  4
Mike                 C                  3
Carley              C                  5
Mark                 C                  1
;

proc sql;
create table want as
select *,sum(fish) as fish_boat
 from have 
  group by boat;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 471 views
  • 0 likes
  • 4 in conversation