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.
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.
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.