BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NewtoSAS00
Fluorite | Level 6

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@NewtoSAS00 :

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. 

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User

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. 


 

NewtoSAS00
Fluorite | Level 6

I want the sum of every 125 observations over the entire time period. 

NewtoSAS00
Fluorite | Level 6
Thank you.
Cynthia_sas
SAS Super FREQ

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.

NewtoSAS00
Fluorite | Level 6

I have the observations sorted and want to calculate the sum of every 125 observations throughout the time period. 

ballardw
Super User

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?

NewtoSAS00
Fluorite | Level 6

I have the data in SAS and want to calculate the sum of every 125 observations for the time period. 

hashman
Ammonite | Level 13

@NewtoSAS00 :

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. 

NewtoSAS00
Fluorite | Level 6

@hashman thank you so much!

hashman
Ammonite | Level 13

@NewtoSAS00 : Welcome.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 1327 views
  • 4 likes
  • 5 in conversation