Hi, I'm new to SAS and wondered if anyone can help. I have a dataset with 500 observations per year, over a 10 year period. I want to add observations 1-125, 126-250, 251-375, 376-500 for each year over the entire period. If there's any guidance you can provide, it'd be much appreciated.
Since you don't show a subset of your sample data, I've mocked it up assuming that the value you want to sum up is called VALUE. This data set HAVE is intrinsically sorted, just as you say you file is; hence to get what you want, you can code:
data have ;
do year = 1 to 10 ;
do value = 1 to 500 ;
output ;
end ;
end ;
run ;
data want (drop = value) ;
do _n_ = 1 to 125 ;
set have (keep = year value) ;
sumvalue = sum (sumvalue, value) ;
end ;
run ;
This will give you 5000/125=40 records with the summary values. If you want to tag the summary values SUMVALUE to the original records, repeat the DOW loop:
data want ;
do _n_ = 1 to 125 ;
set have (keep = value) ;
sumvalue = sum (sumvalue, value) ;
end ;
do _n_ = 1 to 125 ;
set have ;
output ;
end ;
run ;
Kind regards
Paul D.
Per your description, you already have that dataset (5000 observations), so what do you want to add?
@NewtoSAS00 wrote:
Hi, I'm new to SAS and wondered if anyone can help. I have a dataset with 500 observations per year, over a 10 year period. I want to add observations 1-125, 126-250, 251-375, 376-500 for each year over the entire period. If there's any guidance you can provide, it'd be much appreciated.
I want the sum of every 125 observations over the entire time period.
Oh, that's what you meant by "add". Well, you got your answer, so all that ends well is well 😉
Hi: Observations are not "numbered" in a SAS dataset in storage. So the question is do you need to CHANGE observations that are already there? If so, then that is a more complex solution. But if you only need to ADD observations that do not exist in the data, then all you need to do is concatenate or append the data.
If you need for the observations to be in a certain order, you've got to pick something other than observation number, because that is just an added bit of information in PROC PRINT and other PROCS. Observation number can change based on the sorted order of the data rows. So observation 1-250 sorted by country, for example, will be different than observation 1-250 sorted by name
Cynthia.
I have the observations sorted and want to calculate the sum of every 125 observations throughout the time period.
Do you have your data in SAS already?
or are you asking how to read the files you may have into SAS data sets?
or how to combine existing SAS data sets?
I have the data in SAS and want to calculate the sum of every 125 observations for the time period.
Since you don't show a subset of your sample data, I've mocked it up assuming that the value you want to sum up is called VALUE. This data set HAVE is intrinsically sorted, just as you say you file is; hence to get what you want, you can code:
data have ;
do year = 1 to 10 ;
do value = 1 to 500 ;
output ;
end ;
end ;
run ;
data want (drop = value) ;
do _n_ = 1 to 125 ;
set have (keep = year value) ;
sumvalue = sum (sumvalue, value) ;
end ;
run ;
This will give you 5000/125=40 records with the summary values. If you want to tag the summary values SUMVALUE to the original records, repeat the DOW loop:
data want ;
do _n_ = 1 to 125 ;
set have (keep = value) ;
sumvalue = sum (sumvalue, value) ;
end ;
do _n_ = 1 to 125 ;
set have ;
output ;
end ;
run ;
Kind regards
Paul D.
@hashman thank you so much!
@NewtoSAS00 : Welcome.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.