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
@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 ;
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.
@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 ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.