Date: Sun, 30 Apr 2006 17:31:14 +0000
Reply-To: toby dunn <tobydunn@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: toby dunn <tobydunn@HOTMAIL.COM>
Subject: Re: How to automatically change data name in SET?
In-Reply-To: <20060430163825.NHDR25390.ibm61aec.bellsouth.net@sasholewmis4a7>
Content-Type: text/plain; format=flowed
Paul ,
The use of %sysfunc (month(%sysfunc (date()))) concerns me much. As one is
restricted to the data being called is only within one year where the first
data set starts in january and all subsequent months have a dataset. IF
that doesnt hold true then the macro fails. If you dont mind I have a use
for the macro in a paper I am writing on macro design and would like to use
it.
The problem as I see it is a data structure one not a macro problem. If the
data was all in one data set with a variable distinguishing date then all
would be fixed and there would have been no need for a macro to begin with
just possibly a where clause.
If the conditions I mentioned earlier dont hold true then Greggs sql
solution would be better or even you call execute solution would be prefered
to create one big data set and store the information that way.
Toby Dunn
From: "Paul M. Dorfman" <sashole@BELLSOUTH.NET>
Reply-To: sashole@bellsouth.net
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: How to automatically change data name in SET?
Date: Sun, 30 Apr 2006 12:38:22 -0400
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!
>