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 ;             

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!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 556 views
  • 2 likes
  • 3 in conversation