BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10
data a;
  input patientid  start1_date : mmddyy10. ;
  format  start1_date  mmddyy10.;
datalines;
1 5/5/2009 
2 6/6/2010
3 7/7/2011
4 5/5/2011 
5 6/6/2012 



;
run;

I have the above data, I am trying to create a subset for each year individually:

Output

data09 would include

1 5/5/2009

data10 include

2 6/6/2010

data11 include

3 7/7/2011

4 5/5/2011

and so on 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @lillymaginta 

 

@PaigeMiller  's recommendation is absolute and true. However, for what it's worth

 


data a;
  input patientid  start1_date : mmddyy10. ;
  format  start1_date  mmddyy10.;
datalines;
1 5/5/2009 
2 6/6/2010
3 7/7/2011
4 5/5/2011 
5 6/6/2012 
;

data  temp;
set a;
year=year(start1_date);
run;

proc sql ;                                                              
  create index year on temp (year) ;                      
quit ;                                                                  
                                                                        
data _null_ ;                                                           
  if _n_ = 1 then do ;                                                  
    dcl hash h () ;                                                     
    h.definekey  ("_n_") ;                                              
    h.definedata ('patientid','start1_date','year') ;
    h.definedone () ;                                                   
  end ;                                                                 
  do _n_ = 1 by 1 until (last.year) ;                            
    set temp ;                                                          
    by year ;                                                    
    h.add() ;                                                           
  end ;                                                                 
  h.output (dataset: catx ("_", "year",year)) ;          
  h.clear() ;                                                           
run ;             

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

This type of splitting of data is generally not recommended, and you would be (in almost all cases that I know of) keeping the data in one single dataset. it is more work (programming a loop to handle each year) and little benefit. If the data is all together, you can analyze the data by year by using the BY statement in almost any PROC.

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @lillymaginta 

 

@PaigeMiller  's recommendation is absolute and true. However, for what it's worth

 


data a;
  input patientid  start1_date : mmddyy10. ;
  format  start1_date  mmddyy10.;
datalines;
1 5/5/2009 
2 6/6/2010
3 7/7/2011
4 5/5/2011 
5 6/6/2012 
;

data  temp;
set a;
year=year(start1_date);
run;

proc sql ;                                                              
  create index year on temp (year) ;                      
quit ;                                                                  
                                                                        
data _null_ ;                                                           
  if _n_ = 1 then do ;                                                  
    dcl hash h () ;                                                     
    h.definekey  ("_n_") ;                                              
    h.definedata ('patientid','start1_date','year') ;
    h.definedone () ;                                                   
  end ;                                                                 
  do _n_ = 1 by 1 until (last.year) ;                            
    set temp ;                                                          
    by year ;                                                    
    h.add() ;                                                           
  end ;                                                                 
  h.output (dataset: catx ("_", "year",year)) ;          
  h.clear() ;                                                           
run ;             

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 918 views
  • 2 likes
  • 3 in conversation