Date: Sun, 30 Apr 2006 12:38:22 -0400
Reply-To: sashole@bellsouth.net
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Paul M. Dorfman" <sashole@BELLSOUTH.NET>
Organization: Sashole of Florida
Subject: Re: How to automatically change data name in SET?
In-Reply-To: <1146409449.490371.74320@g10g2000cwb.googlegroups.com>
Content-Type: text/plain; charset="us-ascii"
Lei,
First of all, I do not think you really want to achieve the effect of
SET SUBRO.MYDATA1;
SET SUBRO.MYDATA2;
...
SET SUBRO.MYDATA5;
But rather
SET SUBRO.MYDATA1
SUBRO.MYDATA2
SUBRO.MYDATA3
;
Please research the difference - it is very important. Now if I can assume that
the month you are talking about is the current calendar month, then its value
can be obtained using a SAS function. As for the "dynamic" DATA step, it could
generated in a variety of ways. A macro is one:
%macro cat (lib=, mem= ) ;
%local i ;
%do i = 1 %to %sysfunc (month(%sysfunc (date()))) ;
&lib..&mem&I
%end ;
%mend cat ;
data subro.result ;
set %cat (lib=work, mem=mydata) ;
run ;
CALL EXECUTE is another one:
data _null_ ;
call execute ('data subro.result ;') ;
call execute ('set') ;
do _n_ = 1 to month (date ()) ;
call execute ('subro.mydata' || put (_n_, 2.-L)) ;
end ;
call execute (';') ;
call execute ('run ;') ;
run ;
Then again, there is always the old as ages "tempfile" method:
filename sascode temp ;
data _null_ ;
file sascode ;
put @3 'set ' @ ;
do _n_ = 1 to month (date ()) ;
put @7 'subro.mydata' _n_ ;
end ;
put @3 ' ;' ;
run ;
data subro.result ;
%include sascode ;
run ;
Alternatively, you can write the entire step to SASCODE and include it:
filename sascode temp ;
data _null_ ;
file sascode ;
put @1 'data subro.result ;' ;
put @3 'set ' @ ;
do _n_ = 1 to month (date ()) ;
put @7 'subro.mydata' _n_ ;
end ;
put @3 ' ;' ;
put @1 'run ; ' ;
run ;
%include sascode ;
This has the advantage of being able to be wrapped in a macro just for the sake
of parameterization. Over the pure macro, such a combination provides another
advantage - if a run_time error occurs, the log points to it as usual instead of
the line where the macro is invoked. To wit:
%macro tfcat ( inlib = work
, outlib = work
, inmem =
, outmem =
) ;
filename sascode temp ;
data _null_ ;
file sascode ;
put @1 "data &outlib..&outmem ;" ;
put @3 "set " @ ;
do _n_ = 1 to month (date ()) ;
put @7 "&inlib..&inmem" _n_ ;
end ;
put @3 " ;" ;
put @1 "run ; " ;
run ;
%include sascode / source2 ;
filename sascode clear ;
%mend tfcat ;
%tfcat (inlib=subro, outlib=subro, inmem=mydata, outmem=mydata)
I will leave SCL suggestions how to implement exactly the same to eager experts.
Kind regards
------------
Paul Dorfman
Jax, FL
------------
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On
> Behalf Of lei
> Sent: Sunday, April 30, 2006 11:04 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: How to automatically change data name in SET?
>
> Hi everyone,
>
> I have a question. I have a few datasets, say mydata1,
> mydata2, mydata3, till mydata&M where M is dymically changing
> according to month. For example in March M=3, in April M=4.
> So I wrote a program like this but got error message
>
> DATA SUBRO.RESULT;
> DO I = 1 TO &m;
> SET SUBRO.MYDATA&I;
> END
> RUN;
>
> The effect I want to achieve is that, for example, for m = 3
> DATA SUBRO.RESULT;
> SET SUBRO.MYDATA1;
> SET SUBRO.MYDATA2;
> SET SUBRO.MYDATA3;
> RUN;
>
> for m = 4, it becomes
> DATA SUBRO.RESULT;
> SET SUBRO.MYDATA1;
> SET SUBRO.MYDATA2;
> SET SUBRO.MYDATA3;
> SET SUBRO.MYDATA4;
> RUN;
>
> Is there any way to do this?
>
> Thank you!
>