v8.2 macro question - passing the contents of a data step

v8.2 macro question - passing the contents of a data step

Post by Puddin' M » Sat, 04 Aug 2001 03:51:15



variabl              e to a macro
Date: Thu, 02 Aug 2001 18:51:15 +0000
Content-Type: text/plain; charset="iso-8859-1"


> > Adding call symput( ) doesn't help since the symput'ed variable isn't
> > available until the data step completes.
> This statement is not true.

The statement is not 100% true.

Quote:> I prepared an example to demonstrate that the symput'ed value is available
> at once.

> DATA test;
> DO i=1 TO 10;
> OUTPUT;
> END;
> RUN;
> %LET test=0;
> DATA _null_;
> SET test;
> test1 = SYMGET( 'test' );
> CALL SYMPUT( 'test',LEFT( PUT( i,8. )));
> test2 = SYMGET( 'test' );
> PUT test1= i= test2=;
> RUN;

> OUTPUT:
> test1=0 i=1 test2=1
> test1=1 i=2 test2=2
> test1=2 i=3 test2=3
> test1=3 i=4 test2=4
> test1=4 i=5 test2=5
> test1=5 i=6 test2=6
> test1=6 i=7 test2=7
> test1=7 i=8 test2=8
> test1=8 i=9 test2=9
> test1=9 i=10 test2=10

This proves that the statement is not 100% true.
Neither is it 100% false (see log below).

Quote:> As you see with SYMGET and SYMPUT you get always the correct values.

But it's circular. You take the value of a data step var,
write it to a macro var, then write the same value from
the mac var to a different data step var. If the value
needed to be used in the data step, it was available
without SYMPUT/SYMGET.

Consider the following:

216  %let mv = initial value;
217  data a;
218   x=1; output;
219   x=2; output;
220   run;

NOTE: The data set WORK.A has 2 observations and 1 variables.
NOTE: DATA statement used:
      real time           0.03 seconds
      cpu time            0.03 seconds

221
222  data _null_;
223   set a end=e1;
224   call symput('mv', put(x,2.));
225   t1 =  symget('mv');
226   t2 = "&mv";
227   put _n_= '&mv=' "&mv" +2 x= t1= t2=;
228   if e1 then put / 'EOF:' _n_= '&mv=' "&mv" +2 x= t1= t2=;
229   run;

_N_=1 &mv=initial value  x=1 t1=1 t2=initial value
_N_=2 &mv=initial value  x=2 t1=2 t2=initial value

EOF:_N_=2 &mv=initial value  x=2 t1=2 t2=initial value
NOTE: There were 2 observations read from the dataset WORK.A.
NOTE: DATA statement used:
      real time           0.02 seconds
      cpu time            0.02 seconds

230  %put mv=&mv;
mv= 2

As you can see, it is not possible to transfer the value
of x to a mac var _and_ freely refer to it (as &mv)
in the same data step.

Methinks perhaps there is a global macro reference table
that is not updated except at a step boundary. The CALL
SYMPUT is maybe writing to some kinda shadow buffer
that can be read by SYMGET for the duration of the
data step. Quite possibly Ian and/or Ron have more
learned explanations ...

  Puddin'

*******************************************************

*******************************************************
*** (vigourously boycotting the Enhanced Editor)  *****
******************************************************;

- Show quoted text -



> > Listers:

> > In v8.2, is it possible to invoke a macro within a data step and pass to
> the
> > macro the contents of a data step variable defined within that datastep?
> > Note that the following silliness is for illustration only, the real
macro
> > has some meat that requires it to be a macro.

 
 
 

v8.2 macro question - passing the contents of a data step

Post by Andreas Gruening » Sat, 04 Aug 2001 16:23:19


Dear Sir

You made a common mistake.
A source code line like this

Quote:> 226   t2 = "&mv";

is translated by the macro processor to
Quote:> 226   t2 = "initial value";
> 227   put _n_= '&mv=' "initial value" +2 x= t1= t2=;

and this means that in each round the value of t2 is set to "initial
value" and in the PUT statement the literal is set to "initial value".
The macro global table is updated in each round of the data step but
you don't ask for the actual value of the macro variable in the
specific round of the data step.

With SYMGET('mv') you ask for this actual value.
Your program inserts the value of &mv before compiling and running the
data step.
My program gets in each round the actual value of &mv.

Quote:> 216  %let mv = initial value;
> 222  data _null_;
> 223   set a end=e1;
> 224   call symput('mv', put(x,2.));
> 225   t1 =  symget('mv');
> 226   t2 = "&mv";
> 227   put _n_= '&mv=' "&mv" +2 x= t1= t2=;
> 228   if e1 then put / 'EOF:' _n_= '&mv=' "&mv" +2 x= t1= t2=;
> 229   run;


 
 
 

1. v8.2 macro question - passing the contents of a data step variabl e to a macro

Listers:

In v8.2, is it possible to invoke a macro within a data step and pass to the
macro the contents of a data step variable defined within that datastep?
Note that the following silliness is for illustration only, the real macro
has some meat that requires it to be a macro.

e.g.
%global other;
%macro dumb_macro(stuff);
    %let other = %scan(&stuff,1,'/');
%mend;

data harry;
   input 'c:\test.txt';
   input var1 var2 var3;
   %dumb_macro(var1);  /* passes the literial 'var1' rather than contents of
var1 */
   var4 = "&other";
run;

Adding call symput( ) doesn't help since the symput'ed variable isn't
available until the data step completes.

Am I destined to do something like the following:

data harry;
   retain x 0;
   infile 'c:\blah.txt' end=eof;
   input var1 var2 var3;
   call symput('var'||trim(left(x)),var1);
   if eof then call symput('limit',x);
run;

%macro loop;
%local x;
data second;
 %do x = 1 %to &limit;
     %dumb_macro(&&var&x);
      var4 = "&other";
      output;
   %end;
run;
%mend loop;
%loop;

then merge datasets 'harry' and 'second'.

TIA

2. Apple IIE card for Macs question answered

3. v8.2 macro question - passing the contents of a data step var iable to a macro

4. supplier in UK for Ultra 5 SCSI cards

5. v8.2 macro question - passing the contents of a data step var iabl e to a macro

6. admin question

7. Thanks - v8.2 macro question - passing the contents of a data ste p variabl e to a macro

8. Suticase ][ - Where is it???

9. A question about macro invocation within data step

10. SYSPARM: MACRO v Data Step question

11. Help: passing cell contents to goto in a macro?

12. One pass to find max and maxcnt using data step?