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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Linlin
Lapis Lazuli | Level 10

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

Shayan2012
Quartz | Level 8

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!

stat_sas
Ammonite | Level 13

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;

art297
Opal | Level 21

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;

Shayan2012
Quartz | Level 8

Thanks a lot, Arthur. Actually this is what I was looking for!

Patrick
Opal | Level 21

There are multiple ways of how you can split a single data set into multiple ones - but it's most of the time not a good idea. As points out most tasks are better done using by group processing.

Can you explain us why you want to split the data set?

Shayan2012
Quartz | Level 8

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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 8 replies
  • 1063 views
  • 8 likes
  • 6 in conversation