DATA Step, Macro, Functions and more

Is it possible to make a list in macro statements?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Is it possible to make a list in macro statements?

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!


Accepted Solutions
Solution
‎05-16-2014 03:04 PM
Super Contributor
Posts: 1,636

Re: Is it possible to make a list in macro statements?

Posted in reply to Shayan2012

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


All Replies
Trusted Advisor
Posts: 1,913

Re: Is it possible to make a list in macro statements?

Posted in reply to Shayan2012

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.

Solution
‎05-16-2014 03:04 PM
Super Contributor
Posts: 1,636

Re: Is it possible to make a list in macro statements?

Posted in reply to Shayan2012

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

Frequent Contributor
Posts: 75

Re: Is it possible to make a list in macro statements?

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!

Trusted Advisor
Posts: 1,228

Re: Is it possible to make a list in macro statements?

Posted in reply to Shayan2012

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;

PROC Star
Posts: 7,468

Re: Is it possible to make a list in macro statements?

Posted in reply to Shayan2012

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 ' ',

         Smiley Surprisedut separated by ' '

      from have

  ;

quit;

data &years;

  set have;

  select (year(date));

    &out.

    otherwise;

  end;

run;

Frequent Contributor
Posts: 75

Re: Is it possible to make a list in macro statements?

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

Respected Advisor
Posts: 4,173

Re: Is it possible to make a list in macro statements?

Posted in reply to Shayan2012

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?

Frequent Contributor
Posts: 75

Re: Is it possible to make a list in macro statements?

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!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 317 views
  • 8 likes
  • 6 in conversation