Hi all,
I have a small question actually, and I am asking that to widen my knowledge of sas. So, I want to break my data set year by year in different datasets. This is the small code that I have:
%let yearvar=1991;
data have&yearvar;
set have;
if year(date)=&yearvar;
run;
So, It is just enough to change the year every time to get for every year. However, I was thinking if there is a way to make a list in the %let statement, that sas does the thing for every one in the list. I could not find such thing in my searches, But I think it might be useful in some cases.
Any one has any idea?
Thanks a lot!
you could create a macro:
data have;
input date mmddyy10.;
format date mmddyy10.;
cards;
1/1/2001
2/1/2001
1/1/2002
1/1/2006
;
proc sql noprint;
select distinct year(date) into :year separated by ' '
from have;
quit;
%macro test;
%do i=1 %to %sysfunc(countw(&year));
%let n=%scan(&year,&i);
data have&n;
set have(where=(year(date)=&n));
run;
%end;
%mend;
%test
Without knowing what comes next after the data step, my guess is that you really want to use a BY statement in whatever PROC follows, and then all this splitting of data sets is unnecessary.
But you could certainly loop through all years in &yearvar and create multiple data sets if you wanted.
you could create a macro:
data have;
input date mmddyy10.;
format date mmddyy10.;
cards;
1/1/2001
2/1/2001
1/1/2002
1/1/2006
;
proc sql noprint;
select distinct year(date) into :year separated by ' '
from have;
quit;
%macro test;
%do i=1 %to %sysfunc(countw(&year));
%let n=%scan(&year,&i);
data have&n;
set have(where=(year(date)=&n));
run;
%end;
%mend;
%test
Thanks a lot linlin,
This was exactcly what I wanted to learn. Actually I was not aware of " select into " and after reading some documents I see how powerful it is.
thanks again!
Try this.
data have;
input Company $ Year;
datalines;
A 2001
A 2002
A 2003
A 2004
B 2001
B 2002
B 2003
C 2001
C 2002
C 2003
C 2004
;
%macro yr;
%do i=2001 %to 2004;
data year&i;
set have(where=(year=&i));
%end;
run;
%mend yr;
%yr;
Not sure if the following is what you are trying to do but, if it is, this would be one way to do it:
data have;
input date date9. x;
cards;
01jan1990 1
02jan1990 2
01jan1991 1
02jan1991 2
01jan1992 1
02jan1992 2
;
proc sql noprint;
select distinct catt('have',year(date)),
catt('when (',year(date),') output have',year(date),';')
into :years separated by ' ',
:out separated by ' '
from have
;
quit;
data &years;
set have;
select (year(date));
&out.
otherwise;
end;
run;
Thanks a lot, Arthur. Actually this is what I was looking for!
Thanks a lot Patrick,
Actually at first I decided to do so because the whole data set was too large and other than losing track, running it would also take a lot of time. Also, It made me to work more on my macro programming skills!
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: