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

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

Post by Droogendyk, Har » Fri, 03 Aug 2001 01:26:50



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

 
 
 

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

Post by Ac » Fri, 03 Aug 2001 01:33:30




>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.

<snip>

Without checking this, I'd say definitely not. It's inherent in the
concept of SAS macros that this could never work.

Particularly when, as I infer from your example, the contents of said
macro want to include a separate data step.

Instead, you should consider writing program steps to a file or using
call execute.

e.g. (untested)

data harry;
   input 'c:\test.txt';
   input var1 var2 var3;
length string $ 200 ;
string = '%dumb_macro(' || trim(left(var1)) || ') ;' ;
call execute(string) ;
   var4 = "&other";
run;

HTH

--
Ace in Basel

 
 
 

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

Post by Andreas Grueninge » Fri, 03 Aug 2001 21:03:28


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

This statement is not true.
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
As you see with SYMGET and SYMPUT you get always the correct values.

What do you want to achieve?



Quote:> 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 variabl e to a macro

Post by Andreas Gruening » Sat, 04 Aug 2001 17:40:05


Hey Harry,

I showed you and Puddin'Man in a private email that the following
statement is definitely not true
"Adding call symput( ) doesn't help since the symput'ed variable isn't
available until the data step completes.".

Then I asked you what do you want to achieve with your program because
you did not explain it. And you did not answer.

"Therein lies the real beauty of this list; the ability of folks to
interpret
the problem and suggest a real solution rather than the sometimes
half-baked
one that promoted the question in the first place."
If you would like to indicate that me is under the latter folks (the
half-baked ones) I really can't follow you.

Sincerely yours

 
 
 

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

Post by Richard DeVenezi » Sun, 05 Aug 2001 02:20:05


Sample culled from an earlier post.

When you understand the following you will be well on your way to painless
macro variable use.

-----

%let a = Value of a before data step compiled;

data a;
  call symput ('a', 'Value of a assigned during data step');

  * single quote prevents macro variable resolution at data step compile;
  macroA0 = '&a';
  put macroA0=;

  * double quote allows macro variable resolution at data step compile;
  macroA1 =  "&a";
  put macroA1=;

  * symget reads current macro variable value;
  macroA2 = symget ('a');
  put macroA2=;

  * single quote prevents macro variable resolution at data step compile;
  * hence string with ampersand is resolved at run-time;
  macroA3 = resolve ('&a');
  put macroA3=;

  * double quote allows macro variable resolution at data step compile;
  * since _a_ is resolved at compile time, the value of _a_ prior to the
datastep
  * is passed to resolve;
  macroA4 = resolve ("&a");
  put macroA4=;
run;

--
Richard DeVenezia - SAS Macros and AF Tools
http://www.devenezia.com


Quote:> 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

 
 
 

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

Andreas:

No, no, I was the one with the half-baked solution.  I was merely pointing
out that I was attempting to solve a problem one way ( half-baked because it
didn't work, mixing macro and data step ) and Puddin' Man suggested an
entirely new solution that did work.

Please understand that I'm appreciative of each suggestion / solution
provided; each one is an opportunity to broaden my knowledge of SAS.

Regards,
Harry

2. Server Error: Unknown web server type "msiis"

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

4. Superunitary speedups

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

6. ezmlm warning

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

8. When the hell MAME .29 out?

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. Macro-Problem: Writing macro-variables into a data-set

13. run macro in data step