A question about macro invocation within data step

A question about macro invocation within data step

Post by Pau » Tue, 03 Dec 2002 22:34:13



To the SASers:

Could someone please help me out here?

Here's my problem.

I  have created a  macro testmac(par1,par2,par3) that returns a single
value. I also have a dataset mydata with some variables v1,v2,v3 .....

Now I would like to loop thru mydata, invoking testmac each time with the
variables v1,v2,v3 and store the result as a new variable in the same
dataset.

The basic idea should be something like this:

data mydata;
 set mydata;
/* invoke macro with variables v1,v2,v3 of  each obs
 newvar=%testmac(v1,v2,v3)
run;

after excution of  the code

proc print data=mydata;
run;

should now give

v1   v2   v3   newvar other variables
98     58       68    1    .......
75     100    83     4    ........
.....    ...  ..    ... .. ............

Any help would be appreciated.

Paul

 
 
 

A question about macro invocation within data step

Post by Jim Groeneve » Tue, 03 Dec 2002 23:04:32


Hi Paul,

That is only possibly if your macro only consists of macro code, no data
step code. An example of such application is:

CALL EXECUTE ('%LET NewVar = %TestMac(' || V1char || ',' || V2char || ','
V3char || ');' );
NewVar = SYMGET ('NewVar');

in which V1char (etc.) is the character variable form of V1, e.g. if V1 is
numeric it is:
TRIM(COMPRESS(PUT (V1, BEST12.)))

Does this help you?

Regards - Jim.
--
Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31 412 407 070
senior statist./data man.   P.O. Box 1            fax. +31 412 407 080

My computer does what I tell it to do; sometimes it says my instructions are
illegal.

Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.

> -----Original Message-----

> Sent: 02 December 2002 14:34

> Subject: A question about macro invocation within data step

> To the SASers:

> Could someone please help me out here?

> Here's my problem.

> I  have created a  macro testmac(par1,par2,par3) that returns a single
> value. I also have a dataset mydata with some variables v1,v2,v3 .....

> Now I would like to loop thru mydata, invoking testmac each
> time with the
> variables v1,v2,v3 and store the result as a new variable in the same
> dataset.

> The basic idea should be something like this:

> data mydata;
>  set mydata;
> /* invoke macro with variables v1,v2,v3 of  each obs
>  newvar=%testmac(v1,v2,v3)
> run;

> after excution of  the code

> proc print data=mydata;
> run;

> should now give

> v1   v2   v3   newvar other variables
> 98     58       68    1    .......
> 75     100    83     4    ........
> .....    ...  ..    ... .. ............

> Any help would be appreciated.

> Paul


 
 
 

A question about macro invocation within data step

Post by Jim Groeneve » Tue, 03 Dec 2002 23:17:26


Hi Harry,

Yes that works if you have a macro that contains (only) data step code. What
happens is that at compile time of the data step SAS code the macro calls
are replaced by their contents, i.e. the TRIM etc. function, which results
in the following valid data step statement:

NewVar = TRIM(PUT(V1 + ( V2 * V3 ),BEST12.));
NewVar will be of type character.

However, this does not work if you have exclusively macro code that does not
yeild SAS code, but a macro value as the result. Then you have to apply the
delaying solution using CALL EXECUTE and SYMGET.

Regards - Jim.
--
Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31 412 407 070
senior statist./data man.   P.O. Box 1            fax. +31 412 407 080

My computer does what I tell it to do; sometimes it says my instructions are
illegal.

Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.

> -----Original Message-----

> Sent: 02 December 2002 15:06
> To: 'Jim Groeneveld'
> Subject: RE: A question about macro invocation within data step

> Jim:

> I don't understand, this works, it contains data step code:

> data a;
>   do v1 = 1 to 3;
>     do v2 = 4 to 5;
>       do v3 = 6 to 8;
>         output;
>       end;
>     end;
>   end;
> run;

> %macro testmac(v1=,v2=,v3=);

>    trim(put(&v1 + ( &v2 * &v3 ),best12.))

> %mend testmac;

> data b;
>   set a;
>   newvar = %testmac(v1=v1,v2=v2,v3=v3);
> run;

> proc print;run;

>               -----Original Message-----
>               From:   Jim Groeneveld

>               Sent:   Monday, December 02, 2002 9:05 AM

>               Subject:        Re: A question about macro
> invocation within
> data step

>               Hi Paul,

>               That is only possibly if your macro only
> consists of macro
> code, no data
>               step code. An example of such application is:

>               CALL EXECUTE ('%LET NewVar = %TestMac(' ||
> V1char || ',' ||
> V2char || ','
>               V3char || ');' );
>               NewVar = SYMGET ('NewVar');

>               in which V1char (etc.) is the character
> variable form of V1,
> e.g. if V1 is
>               numeric it is:
>               TRIM(COMPRESS(PUT (V1, BEST12.)))

>               Does this help you?

>               Regards - Jim.
>               --
>               Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO
>   tel. +31
> 412 407 070
>               senior statist./data man.   P.O. Box 1
>   fax. +31
> 412 407 080

> www.imrotramarko.com

>               My computer does what I tell it to do;
> sometimes it says my
> instructions are
>               illegal.

>               Notice of confidentiality: this e-mail may contain
> confidential information
>               intended for the addressed recipient only.
>               If you have received this e-mail in error
> please delete this
> e-mail and
>               please notify the sender so that proper delivery can be
> arranged.

>               > -----Original Message-----

>               > Sent: 02 December 2002 14:34

>               > Subject: A question about macro invocation within data
> step

>               > To the SASers:

>               > Could someone please help me out here?

>               > Here's my problem.

>               > I  have created a  macro testmac(par1,par2,par3) that
> returns a single
>               > value. I also have a dataset mydata with some
> variables
> v1,v2,v3 .....

>               > Now I would like to loop thru mydata, invoking testmac
> each
>               > time with the
>               > variables v1,v2,v3 and store the result as a
> new variable
> in the same
>               > dataset.

>               > The basic idea should be something like this:

>               > data mydata;
>               >  set mydata;
>               > /* invoke macro with variables v1,v2,v3 of  each obs
>               >  newvar=%testmac(v1,v2,v3)
>               > run;

>               > after excution of  the code

>               > proc print data=mydata;
>               > run;

>               > should now give

>               > v1   v2   v3   newvar other variables
>               > 98     58       68    1    .......
>               > 75     100    83     4    ........
>               > .....    ...  ..    ... .. ............

>               > Any help would be appreciated.

>               > Paul

 
 
 

A question about macro invocation within data step

Post by Peter Crawfo » Tue, 03 Dec 2002 23:26:40


much depends on the nature of your code in testmac(par1,par2,par3)

If the text generated, does not go through a "statement boundary"
then
   newvar=%testmac(v1,v2,v3) ;
_might_ be valid.

You ask for help.
Is that with the principles of %macro ?
If so, see the on-line doc for base sas concepts
and then
Base SAS Software
  /SAS Macro Language Reference
    /Introduction
      /getting started with the Macro Facility
or go directly to....
http://v8doc.sas.com/sashtml/macro/zetstart.htm
at the si web site for on line doc

It might be appropriate to indicate what you really
want your macro to do.
As you have it so far, we could recommend something
_like_:
 %macro testmac(par1,par2,par3);
   mean( &par1, &par2, &par3 )
%mend  testmac;
Then
   newvar=%testmac(v1,v2,v3);
provides the code
   newvar= mean( v1, v2, v3 ) ;
For this you don't need a macro !

What do you really need this macro for ?

Regards
Peter Crawford

Datum:         02/12/2002 13:44


Betreff:       A question about macro invocation within data step
Nachrichtentext:

To the SASers:

Could someone please help me out here?

Here's my problem.

I  have created a  macro testmac(par1,par2,par3) that returns a single
value. I also have a dataset mydata with some variables v1,v2,v3 .....

Now I would like to loop thru mydata, invoking testmac each time with the
variables v1,v2,v3 and store the result as a new variable in the same
dataset.

The basic idea should be something like this:

data mydata;
 set mydata;
/* invoke macro with variables v1,v2,v3 of  each obs
 newvar=%testmac(v1,v2,v3)
run;

after excution of  the code

proc print data=mydata;
run;

should now give

v1   v2   v3   newvar other variables
98     58       68    1    .......
75     100    83     4    ........
.....    ...  ..    ... .. ............

Any help would be appreciated.

Paul

--

Diese E-Mail enth?lt vertrauliche und/oder rechtlich geschtzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtmlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

 
 
 

A question about macro invocation within data step

Post by Jim Groeneve » Tue, 03 Dec 2002 23:34:01


Hi Harry,

No, if your macro only consists of macro code, you have to CALL EXECUTE and
SYMGET, because macro code is resolved and run before data step code
compiles. So if your macro would be:

%macro testmac(v1=,v2=,v3=);

   %SYSFUNC(trim(%SYSFUNC(put(%EVAL(&v1 + (%EVAL(&v2 * &v3 ))),best12.))))
   /* this yields a macro value, not a normal variable value */

%mend testmac;

then you obviously should apply it delayed, forcing it to execute while
running the data step. If you would not delay execution, the macro would run
once only and try to do some arithmatic with macro values v1, v2 and v3 (the
characters v, 1, 2 and 3), which makes no sense.

Regards - Jim.
--
Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31 412 407 070
senior statist./data man.   P.O. Box 1            fax. +31 412 407 080
J.Groenev...@ITGroups.com   5350 AA BERGHEM, NL   www.imrotramarko.com

My computer does what I tell it to do; sometimes it says my instructions are
illegal.

Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.

> -----Original Message-----
> From: Droogendyk, Harry [mailto:Harry.Droogen...@CIBC.com]
> Sent: 02 December 2002 15:20
> To: 'Jim Groeneveld'
> Subject: RE: A question about macro invocation within data step

> My question arose from your original answer:

>               "That is only possibly if your macro only
> consists of macro
> code, no data step code"

> Evidently you intended something like:  "Only possible if the
> macro consists
> of ONLY data step code, no macro variable results"

>               -----Original Message-----
>               From:   Jim Groeneveld
> [mailto:J.Groenev...@ITGroups.com]
>               Sent:   Monday, December 02, 2002 9:17 AM
>               To:     'Droogendyk, Harry'
>               Cc:     'sa...@listserv.uga.edu'
>               Subject:        RE: A question about macro
> invocation within
> data step

>               Hi Harry,

>               Yes that works if you have a macro that
> contains (only) data
> step code. What
>               happens is that at compile time of the data
> step SAS code
> the macro calls
>               are replaced by their contents, i.e. the TRIM
> etc. function,
> which results
>               in the following valid data step statement:

>               NewVar = TRIM(PUT(V1 + ( V2 * V3 ),BEST12.));
>               NewVar will be of type character.

>               However, this does not work if you have
> exclusively macro
> code that does not
>               yeild SAS code, but a macro value as the
> result. Then you
> have to apply the
>               delaying solution using CALL EXECUTE and SYMGET.

>               Regards - Jim.
>               --
>               Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO
>   tel. +31
> 412 407 070
>               senior statist./data man.   P.O. Box 1
>   fax. +31
> 412 407 080
>               J.Groenev...@ITGroups.com   5350 AA BERGHEM, NL
> www.imrotramarko.com

>               My computer does what I tell it to do;
> sometimes it says my
> instructions are
>               illegal.

>               Notice of confidentiality: this e-mail may contain
> confidential information
>               intended for the addressed recipient only.
>               If you have received this e-mail in error
> please delete this
> e-mail and
>               please notify the sender so that proper delivery can be
> arranged.

>               > -----Original Message-----
>               > From: Droogendyk, Harry
> [mailto:Harry.Droogen...@CIBC.com]
>               > Sent: 02 December 2002 15:06
>               > To: 'Jim Groeneveld'
>               > Subject: RE: A question about macro
> invocation within data
> step

>               > Jim:

>               > I don't understand, this works, it contains data step
> code:

>               > data a;
>               >   do v1 = 1 to 3;
>               >     do v2 = 4 to 5;
>               >       do v3 = 6 to 8;
>               >         output;
>               >       end;
>               >     end;
>               >   end;
>               > run;

>               > %macro testmac(v1=,v2=,v3=);

>               >    trim(put(&v1 + ( &v2 * &v3 ),best12.))

>               > %mend testmac;

>               > data b;
>               >   set a;
>               >   newvar = %testmac(v1=v1,v2=v2,v3=v3);
>               > run;

>               > proc print;run;

>               >               -----Original Message-----
>               >               From:   Jim Groeneveld
>               > [mailto:J.Groenev...@ITGROUPS.COM]
>               >               Sent:   Monday, December 02,
> 2002 9:05 AM
>               >               To:     SA...@LISTSERV.UGA.EDU
>               >               Subject:        Re: A question
> about macro
>               > invocation within
>               > data step

>               >               Hi Paul,

>               >               That is only possibly if your
> macro only
>               > consists of macro
>               > code, no data
>               >               step code. An example of such
> application
> is:

>               >               CALL EXECUTE ('%LET NewVar =
> %TestMac(' ||
>               > V1char || ',' ||
>               > V2char || ','
>               >               V3char || ');' );
>               >               NewVar = SYMGET ('NewVar');

>               >               in which V1char (etc.) is the character
>               > variable form of V1,
>               > e.g. if V1 is
>               >               numeric it is:
>               >               TRIM(COMPRESS(PUT (V1, BEST12.)))

>               >               Does this help you?

>               >               Regards - Jim.
>               >               --
>               >               Y. (Jim) Groeneveld, MSc
> IMRO TRAMARKO

>               >   tel. +31
>               > 412 407 070
>               >               senior statist./data man.   P.O. Box 1

>               >   fax. +31
>               > 412 407 080
>               >               J.Groenev...@ITGroups.com
> 5350 AA BERGHEM,
> NL
>               > www.imrotramarko.com

>               >               My computer does what I tell it to do;
>               > sometimes it says my
>               > instructions are
>               >               illegal.

>               >               Notice of confidentiality: this
> e-mail may
> contain
>               > confidential information
>               >               intended for the addressed
> recipient only.
>               >               If you have received this
> e-mail in error
>               > please delete this
>               > e-mail and
>               >               please notify the sender so that proper
> delivery can be
>               > arranged.

>               >               > -----Original Message-----
>               >               > From: Paul

[mailto:ped015...@UTDALLAS.EDU]
                >               > Sent: 02 December 2002 14:34
                >               > To: SA...@LISTSERV.UGA.EDU
                >               > Subject: A question about macro invocation
within data
                > step
                >               >
                >               >
                >               > To the SASers:
                >               >
                >               > Could someone please help me out here?
                >               >
                >               > Here's my problem.
                >               >
                >               > I  have created a  macro
testmac(par1,par2,par3) that
                > returns a single
                >               > value. I also have a dataset mydata with
some
                > variables
                > v1,v2,v3 .....
                >               >
                >               > Now I would like to loop thru mydata,
invoking testmac
                > each
                >               > time with the
                >               > variables v1,v2,v3 and store the result as
a
                > new variable
                > in the same
                >               > dataset.
                >               >
                >               > The basic idea should be something like
this:
                >               >
                >               > data mydata;
                >               >  set mydata;
                >               > /* invoke macro with variables v1,v2,v3 of
each obs
                >               >  newvar=%testmac(v1,v2,v3)
                >               > run;
                >               >
                >               > after excution of  the code
                >               >
                >               > proc print data=mydata;
                >               > run;
                >               >
                >               > should now give
                >               >
                >               > v1   v2   v3   newvar other variables
                >               > 98     58       68    1    .......
                >               > 75     100    83     4    ........
                >               > .....    ...  ..    ... .. ............
                >               >
                >               > Any help would be appreciated.
                >               >
                >               > Paul
                >               >
...

read more »

 
 
 

A question about macro invocation within data step

Post by Droogendyk, Har » Tue, 03 Dec 2002 23:35:22


Something tells me we're talking about the same thing, but misunderstanding
each other.

Thanks for your thoughts.

                -----Original Message-----
                From:   Jim Groeneveld [mailto:J.Groenev...@ITGroups.com]
                Sent:   Monday, December 02, 2002 9:34 AM
                To:     'Droogendyk, Harry'

                Cc:     'sa...@listserv.uga.edu'
                Subject:        RE: A question about macro invocation within
data step

                Hi Harry,

                No, if your macro only consists of macro code, you have to
CALL EXECUTE and
                SYMGET, because macro code is resolved and run before data
step code
                compiles. So if your macro would be:

                %macro testmac(v1=,v2=,v3=);

                   %SYSFUNC(trim(%SYSFUNC(put(%EVAL(&v1 + (%EVAL(&v2 * &v3
))),best12.))))
                   /* this yields a macro value, not a normal variable value
*/

                %mend testmac;

                then you obviously should apply it delayed, forcing it to
execute while
                running the data step. If you would not delay execution, the
macro would run
                once only and try to do some arithmatic with macro values
v1, v2 and v3 (the
                characters v, 1, 2 and 3), which makes no sense.

                Regards - Jim.
                --
                Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31
412 407 070
                senior statist./data man.   P.O. Box 1            fax. +31
412 407 080
                J.Groenev...@ITGroups.com   5350 AA BERGHEM, NL
www.imrotramarko.com

                My computer does what I tell it to do; sometimes it says my
instructions are
                illegal.

                Notice of confidentiality: this e-mail may contain
confidential information
                intended for the addressed recipient only.
                If you have received this e-mail in error please delete this
e-mail and
                please notify the sender so that proper delivery can be
arranged.

                > -----Original Message-----
                > From: Droogendyk, Harry [mailto:Harry.Droogen...@CIBC.com]
                > Sent: 02 December 2002 15:20
                > To: 'Jim Groeneveld'
                > Subject: RE: A question about macro invocation within data
step

                > My question arose from your original answer:

                >               "That is only possibly if your macro only
                > consists of macro
                > code, no data step code"

                > Evidently you intended something like:  "Only possible if
the
                > macro consists
                > of ONLY data step code, no macro variable results"

                >               -----Original Message-----
                >               From:   Jim Groeneveld
                > [mailto:J.Groenev...@ITGroups.com]
                >               Sent:   Monday, December 02, 2002 9:17 AM
                >               To:     'Droogendyk, Harry'
                >               Cc:     'sa...@listserv.uga.edu'
                >               Subject:        RE: A question about macro
                > invocation within
                > data step

                >               Hi Harry,

                >               Yes that works if you have a macro that
                > contains (only) data
                > step code. What
                >               happens is that at compile time of the data
                > step SAS code
                > the macro calls
                >               are replaced by their contents, i.e. the
TRIM
                > etc. function,
                > which results
                >               in the following valid data step statement:

                >               NewVar = TRIM(PUT(V1 + ( V2 * V3
),BEST12.));
                >               NewVar will be of type character.

                >               However, this does not work if you have
                > exclusively macro
                > code that does not
                >               yeild SAS code, but a macro value as the
                > result. Then you
                > have to apply the
                >               delaying solution using CALL EXECUTE and
SYMGET.

                >               Regards - Jim.
                >               --
                >               Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO

                >   tel. +31
                > 412 407 070
                >               senior statist./data man.   P.O. Box 1

                >   fax. +31
                > 412 407 080
                >               J.Groenev...@ITGroups.com   5350 AA BERGHEM,
NL
                > www.imrotramarko.com

                >               My computer does what I tell it to do;
                > sometimes it says my
                > instructions are
                >               illegal.

                >               Notice of confidentiality: this e-mail may
contain
                > confidential information
                >               intended for the addressed recipient only.
                >               If you have received this e-mail in error
                > please delete this
                > e-mail and
                >               please notify the sender so that proper
delivery can be
                > arranged.

                >               > -----Original Message-----
                >               > From: Droogendyk, Harry
                > [mailto:Harry.Droogen...@CIBC.com]
                >               > Sent: 02 December 2002 15:06
                >               > To: 'Jim Groeneveld'
                >               > Subject: RE: A question about macro
                > invocation within data
                > step

                >               > Jim:

                >               > I don't understand, this works, it
contains data step
                > code:

                >               > data a;
                >               >   do v1 = 1 to 3;
                >               >     do v2 = 4 to 5;
                >               >       do v3 = 6 to 8;
                >               >         output;
                >               >       end;
                >               >     end;
                >               >   end;
                >               > run;

                >               > %macro testmac(v1=,v2=,v3=);

                >               >    trim(put(&v1 + ( &v2 * &v3 ),best12.))

                >               > %mend testmac;

                >               > data b;
                >               >   set a;
                >               >   newvar = %testmac(v1=v1,v2=v2,v3=v3);
                >               > run;

                >               > proc print;run;

                >               >               -----Original Message-----
                >               >               From:   Jim Groeneveld
                >               > [mailto:J.Groenev...@ITGROUPS.COM]
                >               >               Sent:   Monday, December 02,

                > 2002 9:05 AM
                >               >               To:
SA...@LISTSERV.UGA.EDU
                >               >               Subject:        Re: A
question
                > about macro
                >               > invocation within
                >               > data step

                >               >               Hi Paul,

                >               >               That is only possibly if
your
                > macro only
                >               > consists of macro
                >               > code, no data
                >               >               step code. An example of
such
                > application
                > is:

                >               >               CALL EXECUTE ('%LET NewVar =

                > %TestMac(' ||
                >               > V1char || ',' ||
                >               > V2char || ','
                >               >               V3char || ');' );
                >               >               NewVar = SYMGET ('NewVar');

                >               >               in which V1char (etc.) is
the character
                >               > variable form of V1,
                >               > e.g. if V1 is
                >               >               numeric it is:
                >               >               TRIM(COMPRESS(PUT (V1,
BEST12.)))

                >               >               Does this help you?

                >               >               Regards - Jim.
                >               >               --
                >               >               Y. (Jim) Groeneveld, MSc
                > IMRO TRAMARKO

                >               >   tel. +31
                >               > 412 407 070
                >               >               senior statist./data man.
P.O. Box 1

                >               >   fax. +31

...

read more »

 
 
 

A question about macro invocation within data step

Post by Peter Crawfo » Wed, 04 Dec 2002 00:07:16


HI Jim
if it makes sense to use the variable names in the
text generated by the macro, then it must be acceptable
.........OK it isn't the value in the observatiopn that the
 macro sees.............we both understand that
 ...... but it may still be useful to have such a macro
For example a macro that can act like a function
%macro econv( var, rate );
       &var * inputn( &rate, 'myrates.' )
%mend  econv;
might be used in a datastep statement like
     mymoney = %econv( trade, "NLG" );
I can also use it in FSview, like
  where %econv( trade, trade_ccy ) gt 1e6
to extract from a column of values in differing currencies
but all having a common currency value of 1 million or more

Of course. this assumes a common understanding of
the original posters question. I don't think we have that !
yet.

Regards
Peter Crawford

Datum:         02/12/2002 14:33
An:            SA...@LISTSERV.UGA.EDU

Antwort an:    Jim Groeneveld <J.Groenev...@ITGROUPS.COM>

Betreff:       Re: A question about macro invocation within data step
Nachrichtentext:

Hi Harry,

No, if your macro only consists of macro code, you have to CALL EXECUTE and
SYMGET, because macro code is resolved and run before data step code
compiles. So if your macro would be:

%macro testmac(v1=,v2=,v3=);

   %SYSFUNC(trim(%SYSFUNC(put(%EVAL(&v1 + (%EVAL(&v2 * &v3 ))),best12.))))
   /* this yields a macro value, not a normal variable value */

%mend testmac;

then you obviously should apply it delayed, forcing it to execute while
running the data step. If you would not delay execution, the macro would run
once only and try to do some arithmatic with macro values v1, v2 and v3 (the
characters v, 1, 2 and 3), which makes no sense.

Regards - Jim.
--
Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31 412 407 070
senior statist./data man.   P.O. Box 1            fax. +31 412 407 080
J.Groenev...@ITGroups.com   5350 AA BERGHEM, NL   www.imrotramarko.com

My computer does what I tell it to do; sometimes it says my instructions are
illegal.

Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.

> -----Original Message-----
> From: Droogendyk, Harry [mailto:Harry.Droogen...@CIBC.com]
> Sent: 02 December 2002 15:20
> To: 'Jim Groeneveld'
> Subject: RE: A question about macro invocation within data step

> My question arose from your original answer:

>               "That is only possibly if your macro only
> consists of macro
> code, no data step code"

> Evidently you intended something like:  "Only possible if the
> macro consists
> of ONLY data step code, no macro variable results"

>               -----Original Message-----
>               From:   Jim Groeneveld
> [mailto:J.Groenev...@ITGroups.com]
>               Sent:   Monday, December 02, 2002 9:17 AM
>               To:     'Droogendyk, Harry'
>               Cc:     'sa...@listserv.uga.edu'
>               Subject:        RE: A question about macro
> invocation within
> data step

>               Hi Harry,

>               Yes that works if you have a macro that
> contains (only) data
> step code. What
>               happens is that at compile time of the data
> step SAS code
> the macro calls
>               are replaced by their contents, i.e. the TRIM
> etc. function,
> which results
>               in the following valid data step statement:

>               NewVar = TRIM(PUT(V1 + ( V2 * V3 ),BEST12.));
>               NewVar will be of type character.

>               However, this does not work if you have
> exclusively macro
> code that does not
>               yeild SAS code, but a macro value as the
> result. Then you
> have to apply the
>               delaying solution using CALL EXECUTE and SYMGET.

>               Regards - Jim.
>               --
>               Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO
>   tel. +31
> 412 407 070
>               senior statist./data man.   P.O. Box 1
>   fax. +31
> 412 407 080
>               J.Groenev...@ITGroups.com   5350 AA BERGHEM, NL
> www.imrotramarko.com

>               My computer does what I tell it to do;
> sometimes it says my
> instructions are
>               illegal.

>               Notice of confidentiality: this e-mail may contain
> confidential information
>               intended for the addressed recipient only.
>               If you have received this e-mail in error
> please delete this
> e-mail and
>               please notify the sender so that proper delivery can be
> arranged.

>               > -----Original Message-----
>               > From: Droogendyk, Harry
> [mailto:Harry.Droogen...@CIBC.com]
>               > Sent: 02 December 2002 15:06
>               > To: 'Jim Groeneveld'
>               > Subject: RE: A question about macro
> invocation within data
> step

>               > Jim:

>               > I don't understand, this works, it contains data step
> code:

>               > data a;
>               >   do v1 = 1 to 3;
>               >     do v2 = 4 to 5;
>               >       do v3 = 6 to 8;
>               >         output;
>               >       end;
>               >     end;
>               >   end;
>               > run;

>               > %macro testmac(v1=,v2=,v3=);

>               >    trim(put(&v1 + ( &v2 * &v3 ),best12.))

>               > %mend testmac;

>               > data b;
>               >   set a;
>               >   newvar = %testmac(v1=v1,v2=v2,v3=v3);
>               > run;

>               > proc print;run;

>               >               -----Original Message-----
>               >               From:   Jim Groeneveld
>               > [mailto:J.Groenev...@ITGROUPS.COM]
>               >               Sent:   Monday, December 02,
> 2002 9:05 AM
>               >               To:     SA...@LISTSERV.UGA.EDU
>               >               Subject:        Re: A question
> about macro
>               > invocation within
>               > data step

>               >               Hi Paul,

>               >               That is only possibly if your
> macro only
>               > consists of macro
>               > code, no data
>               >               step code. An example of such
> application
> is:

>               >               CALL EXECUTE ('%LET NewVar =
> %TestMac(' ||
>               > V1char || ',' ||
>               > V2char || ','
>               >               V3char || ');' );
>               >               NewVar = SYMGET ('NewVar');

>               >               in which V1char (etc.) is the character
>               > variable form of V1,
>               > e.g. if V1 is
>               >               numeric it is:
>               >               TRIM(COMPRESS(PUT (V1, BEST12.)))

>               >               Does this help you?

>               >               Regards - Jim.
>               >               --
>               >               Y. (Jim) Groeneveld, MSc
> IMRO TRAMARKO

>               >   tel. +31
>               > 412 407 070
>               >               senior statist./data man.   P.O. Box 1

>               >   fax. +31
>               > 412 407 080
>               >               J.Groenev...@ITGroups.com
> 5350 AA BERGHEM,
> NL
>               > www.imrotramarko.com

>               >               My computer does what I tell it to do;
>               > sometimes it says my
>               > instructions are
>               >               illegal.

>               >               Notice of confidentiality: this
> e-mail may
> contain
>               > confidential information
>               >               intended for the addressed
> recipient only.
>               >               If you have received this
> e-mail in error
>               > please delete this
>               > e-mail and
>               >               please notify the sender so that proper
> delivery can be
>               > arranged.

>               >               > -----Original Message-----
>               >               > From: Paul

[mailto:ped015...@UTDALLAS.EDU]
              >               > Sent: 02 December 2002 14:34
              >               > To: SA...@LISTSERV.UGA.EDU
              >               > Subject: A question about macro invocation
within data
              > step
              >               >
              >               >
              >               > To the SASers:
              >               >
              >               > Could someone please help me out here?
              >               >
              >               > Here's my problem.
              >               >
              >               > I  have created a  macro
testmac(par1,par2,par3) that
              > returns a single
              >               > value. I also have a dataset mydata with
some
              > variables
              > v1,v2,v3 .....
              >               >
              >               > Now I would like to loop thru mydata,
invoking testmac
              > each
              >               > time with the
              >               > variables v1,v2,v3 and store the result as
a
              > new variable
              > in the same
              >               > dataset.
              >               >
              >               > The basic idea should be something like
this:
              >               >
              >               > data mydata;
              >               >  set mydata;
              >               > /*
...

read more »

 
 
 

A question about macro invocation within data step

Post by Jim Groeneve » Wed, 04 Dec 2002 00:26:20


Hi Peter,

Of course. In such a case the macro call resolves into
   mymoney = trade * INPUTN ( "NLG", 'myrates.');
and that is legal and valid of course.
In such a case the macro resolves into the above code (at macro run time or
data step compile time) and only later (at data step run time) the code does
the processing (calculation). The macro doesn't do the calculation!

Regards - Jim.
--
Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31 412 407 070
senior statist./data man.   P.O. Box 1            fax. +31 412 407 080

My computer does what I tell it to do; sometimes it says my instructions are
illegal.

Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.

> -----Original Message-----

> Sent: 02 December 2002 16:07
> To: Jim Groeneveld

> Subject: Re: A question about macro invocation within data step

> HI Jim
> if it makes sense to use the variable names in the
> text generated by the macro, then it must be acceptable
> .........OK it isn't the value in the observatiopn that the
>  macro sees.............we both understand that
>  ...... but it may still be useful to have such a macro
> For example a macro that can act like a function
> %macro econv( var, rate );
>        &var * inputn( &rate, 'myrates.' )
> %mend  econv;
> might be used in a datastep statement like
>      mymoney = %econv( trade, "NLG" );
> I can also use it in FSview, like
>   where %econv( trade, trade_ccy ) gt 1e6
> to extract from a column of values in differing currencies
> but all having a common currency value of 1 million or more

> Of course. this assumes a common understanding of
> the original posters question. I don't think we have that !
> yet.

> Regards
> Peter Crawford
> [.........]

 
 
 

A question about macro invocation within data step

Post by Pau » Wed, 04 Dec 2002 00:23:31



>To the SASers:

>Could someone please help me out here?

>Here's my problem.

>I  have created a  macro testmac(par1,par2,par3) that returns a single
>value. I also have a dataset mydata with some variables v1,v2,v3 .....

>Now I would like to loop thru mydata, invoking testmac each time with the
>variables v1,v2,v3 and store the result as a new variable in the same
>dataset.

>The basic idea should be something like this:

>data mydata;
> set mydata;
>/* invoke macro with variables v1,v2,v3 of  each obs
> newvar=%testmac(v1,v2,v3)
>run;

>after excution of  the code

>proc print data=mydata;
>run;

>should now give

>v1   v2   v3   newvar other variables
>98     58       68    1    .......
>75     100    83     4    ........
>.....    ...  ..    ... .. ............

>Any help would be appreciated.

>Paul

Guys thank you fro your quick response:

Here's the macro and 4 separate invocation.

147  /* count the number of odd numbers in a sequence */
148    %macro testmac(v1,v2,v3);
149    %let odd=0;
150    %do  i=1 %to 3;
151     %let c = %EVAL(&&v&i / 2);
152     %if  %EVAL(&c * 2) ne &&v&i %then
153         %let odd=%eval(&odd+1);
154      %else
155         %let odd=&odd;
156    %end;
157    %put The number of odd numbers is &odd ;
158    %mend testmac;
159
160  %testmac(2,4,6);
The number of odd numbers is 0
161  %testmac(1,2,4);
The number of odd numbers is 1
162  %testmac(1,3,4);
The number of odd numbers is 2
163  %testmac(1,3,5);
The number of odd numbers is 3

So it correctly counts the number of odd numbers
in the argument sequence v1,v2,v3.

Now I have modified
157 and 158 as follows:

157 &odd
158 mend;

So that it should return just the &odd which I would then like to
assingn in a data step.

For example if a create a data set as follows

data mydata;
 input v1 v2 v3;
 cards;
 2 4 6
 1 2 4
 1 3 4
 1 3 5
;
run;

I would then like to be able to write the following code

data mydata;
 set mydata;
  numodd=%testmac(v1,v2,v3);
run;

and get a new data set with the new varable numodd.
So
proc print data=mydata;
run;

would then give

v1 v2 v3 numodd
2   4  6   0
1   2  4   1
1   3  4   2
1   3  5   3

My problem is I don't what is the proper way to invoke the macro using
the record variables as arguments.

Thanks

Paul

 
 
 

A question about macro invocation within data step

Post by Droogendyk, Har » Wed, 04 Dec 2002 00:41:33


Paul:

I don't know why you want to use a macro, what you're trying to do is easily
accomplished in "normal" data step code.

newodd = mod(v1,2) + mod(v2,2) + mod(v3,2);

However, if it really must be macro, the following works:

data a;
  do v1 = 1 to 3;
    do v2 = 4 to 5;
      do v3 = 6 to 8;
        output;
      end;
    end;
  end;
run;

%macro testmac(v1=,v2=,v3=);

  mod(&v1,2) + mod(&v2,2) + mod(&v3,2)

%mend testmac;

data b;
  set a;
  newvar = %testmac(v1=v1,v2=v2,v3=v3);
run;

proc print;run;

The SAS System
08:49 Monday, December 2, 2002   5

Obs    v1    v2    v3    newvar

  1     1     4     6       1
  2     1     4     7       2
  3     1     4     8       1
  4     1     5     6       2
  5     1     5     7       3
  6     1     5     8       2
  7     2     4     6       0
  8     2     4     7       1
  9     2     4     8       0
 10     2     5     6       1
 11     2     5     7       2
 12     2     5     8       1
 13     3     4     6       1
 14     3     4     7       2
 15     3     4     8       1
 16     3     5     6       2
 17     3     5     7       3
 18     3     5     8       2

                -----Original Message-----
                From:   Paul [mailto:ped015...@UTDALLAS.EDU]
                Sent:   Monday, December 02, 2002 10:24 AM
                To:     SA...@LISTSERV.UGA.EDU
                Subject:        Re: A question about macro invocation within
data step

                On Mon, 2 Dec 2002 08:34:13 -0500, Paul
<ped015...@UTDALLAS.EDU> wrote:

                >To the SASers:

                >Could someone please help me out here?

                >Here's my problem.

                >I  have created a  macro testmac(par1,par2,par3) that
returns a single
                >value. I also have a dataset mydata with some variables
v1,v2,v3 .....

                >Now I would like to loop thru mydata, invoking testmac each
time with the
                >variables v1,v2,v3 and store the result as a new variable
in the same
                >dataset.

                >The basic idea should be something like this:

                >data mydata;
                > set mydata;
                >/* invoke macro with variables v1,v2,v3 of  each obs
                > newvar=%testmac(v1,v2,v3)
                >run;

                >after excution of  the code

                >proc print data=mydata;
                >run;

                >should now give

                >v1   v2   v3   newvar other variables
                >98     58       68    1    .......
                >75     100    83     4    ........
                >.....    ...  ..    ... .. ............

                >Any help would be appreciated.

                >Paul

                Guys thank you fro your quick response:

                Here's the macro and 4 separate invocation.

                147  /* count the number of odd numbers in a sequence */
                148    %macro testmac(v1,v2,v3);
                149    %let odd=0;
                150    %do  i=1 %to 3;
                151     %let c = %EVAL(&&v&i / 2);
                152     %if  %EVAL(&c * 2) ne &&v&i %then
                153         %let odd=%eval(&odd+1);
                154      %else
                155         %let odd=&odd;
                156    %end;
                157    %put The number of odd numbers is &odd ;
                158    %mend testmac;
                159
                160  %testmac(2,4,6);
                The number of odd numbers is 0
                161  %testmac(1,2,4);
                The number of odd numbers is 1
                162  %testmac(1,3,4);
                The number of odd numbers is 2
                163  %testmac(1,3,5);
                The number of odd numbers is 3

                So it correctly counts the number of odd numbers
                in the argument sequence v1,v2,v3.

                Now I have modified
                157 and 158 as follows:

                157 &odd
                158 mend;

                So that it should return just the &odd which I would then
like to
                assingn in a data step.

                For example if a create a data set as follows

                data mydata;
                 input v1 v2 v3;
                 cards;
                 2 4 6
                 1 2 4
                 1 3 4
                 1 3 5
                ;
                run;

                I would then like to be able to write the following code

                data mydata;
                 set mydata;
                  numodd=%testmac(v1,v2,v3);
                run;

                and get a new data set with the new varable numodd.
                So
                proc print data=mydata;
                run;

                would then give

                v1 v2 v3 numodd
                2   4  6   0
                1   2  4   1
                1   3  4   2
                1   3  5   3

                My problem is I don't what is the proper way to invoke the
macro using
                the record variables as arguments.

                Thanks

                Paul

 
 
 

A question about macro invocation within data step

Post by Peter Crawfo » Wed, 04 Dec 2002 00:52:38


!!!
Masterful, Harry
   accomplished in "normal" data step code.
      newodd = mod(v1,2) + mod(v2,2) + mod(v3,2);

Just as clear thinking about %macro seemed absent,
so to was an understanding of the potential of base sas.

Perhaps it would be "gilding-the-lilly" to propose a
datastep view to derive the new column, since it
appears to have no immediate purpose <g>

Unless of course, again we are being given another
smoke screen to hide the real requirement !

Maybe it is just the training exercise... teacher,
(at utdallas.edu)  feeling short of inspiration,
sends students to sas-l for new ideas !
I wonder what the "required answer" was supposed to be ?

Regards
Peter Crawford

Datum:         02/12/2002 15:41
An:            SA...@LISTSERV.UGA.EDU

Antwort an:    "Droogendyk, Harry" <Harry.Droogen...@CIBC.COM>

Betreff:       Re: A question about macro invocation within data step
Nachrichtentext:

Paul:

I don't know why you want to use a macro, what you're trying to do is easily
accomplished in "normal" data step code.

newodd = mod(v1,2) + mod(v2,2) + mod(v3,2);

However, if it really must be macro, the following works:

data a;
  do v1 = 1 to 3;
   do v2 = 4 to 5;
     do v3 = 6 to 8;
       output;
     end;
   end;
  end;
run;

%macro testmac(v1=,v2=,v3=);

  mod(&v1,2) + mod(&v2,2) + mod(&v3,2)

%mend testmac;

data b;
  set a;
  newvar = %testmac(v1=v1,v2=v2,v3=v3);
run;

proc print;run;

The SAS System
08:49 Monday, December 2, 2002   5

Obs    v1    v2    v3    newvar

  1     1     4     6       1
  2     1     4     7       2
  3     1     4     8       1
  4     1     5     6       2
  5     1     5     7       3
  6     1     5     8       2
  7     2     4     6       0
  8     2     4     7       1
  9     2     4     8       0
 10     2     5     6       1
 11     2     5     7       2
 12     2     5     8       1
 13     3     4     6       1
 14     3     4     7       2
 15     3     4     8       1
 16     3     5     6       2
 17     3     5     7       3
 18     3     5     8       2

              -----Original Message-----
              From:   Paul [mailto:ped015...@UTDALLAS.EDU]
              Sent:   Monday, December 02, 2002 10:24 AM
              To:     SA...@LISTSERV.UGA.EDU
              Subject:        Re: A question about macro invocation within
data step

              On Mon, 2 Dec 2002 08:34:13 -0500, Paul
<ped015...@UTDALLAS.EDU> wrote:

              >To the SASers:

              >Could someone please help me out here?

              >Here's my problem.

              >I  have created a  macro testmac(par1,par2,par3) that
returns a single
              >value. I also have a dataset mydata with some variables
v1,v2,v3 .....

              >Now I would like to loop thru mydata, invoking testmac each
time with the
              >variables v1,v2,v3 and store the result as a new variable
in the same
              >dataset.

              >The basic idea should be something like this:

              >data mydata;
              > set mydata;
              >/* invoke macro with variables v1,v2,v3 of  each obs
              > newvar=%testmac(v1,v2,v3)
              >run;

              >after excution of  the code

              >proc print data=mydata;
              >run;

              >should now give

              >v1   v2   v3   newvar other variables
              >98     58       68    1    .......
              >75     100    83     4    ........
              >.....    ...  ..    ... .. ............

              >Any help would be appreciated.

              >Paul

              Guys thank you fro your quick response:

              Here's the macro and 4 separate invocation.

              147  /* count the number of odd numbers in a sequence */
              148    %macro testmac(v1,v2,v3);
              149    %let odd=0;
              150    %do  i=1 %to 3;
              151     %let c = %EVAL(&&v&i / 2);
              152     %if  %EVAL(&c * 2) ne &&v&i %then
              153         %let odd=%eval(&odd+1);
              154      %else
              155         %let odd=&odd;
              156    %end;
              157    %put The number of odd numbers is &odd ;
              158    %mend testmac;
              159
              160  %testmac(2,4,6);
              The number of odd numbers is 0
              161  %testmac(1,2,4);
              The number of odd numbers is 1
              162  %testmac(1,3,4);
              The number of odd numbers is 2
              163  %testmac(1,3,5);
              The number of odd numbers is 3

              So it correctly counts the number of odd numbers
              in the argument sequence v1,v2,v3.

              Now I have modified
              157 and 158 as follows:

              157 &odd
              158 mend;

              So that it should return just the &odd which I would then
like to
              assingn in a data step.

              For example if a create a data set as follows

              data mydata;
              input v1 v2 v3;
              cards;
              2 4 6
              1 2 4
              1 3 4
              1 3 5
              ;
              run;

              I would then like to be able to write the following code

              data mydata;
              set mydata;
               numodd=%testmac(v1,v2,v3);
              run;

              and get a new data set with the new varable numodd.
              So
              proc print data=mydata;
              run;

              would then give

              v1 v2 v3 numodd
              2   4  6   0
              1   2  4   1
              1   3  4   2
              1   3  5   3

              My problem is I don't what is the proper way to invoke the
macro using
              the record variables as arguments.

              Thanks

              Paul

--

Diese E-Mail enth?lt vertrauliche und/oder rechtlich geschtzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtmlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

 
 
 

A question about macro invocation within data step

Post by Peter Crawfo » Wed, 04 Dec 2002 01:12:34


"Now I have modified
"157 and 158 as follows:
"
"157 &odd
"158 mend;
"

you still need the '%' on the mend of the macro
Once that is reinstated, you would still have a
problem with
 %let c = %EVAL(&&v&i / 2);
I think this is intended to divide the value in v&i by 2

Unfortunately, that %eval() will execute at "compile-time"
So it will not divide the value _in_ v&i by 2
When say, &v1 contains 3 then the %eval() will work
but when &v1 contains "v1" (even without the quotes)
there is a problem that it is no longer a number. If you
want the value in "v1" then you have to wait for the data
step to run........
............ and by then all macro processing is finished.

I think you need to read the "introduction to swimming"
before diving in at the "deep end"

As you originally say
"My problem is I don't what is the proper way to invoke the macro using
"the record variables as arguments.

therefore, beware !

You really have to distinguish between variable values
and the variable names.
That is the parallel between data step code and macro code

A macro generates code, before the code runs.

Anything derived by the code, cannot contribute
to the macro generating the code.

............
good luck
Peter Crawford

Datum:         02/12/2002 15:33


Betreff:       Re: A question about macro invocation within data step
Nachrichtentext:


>To the SASers:

>Could someone please help me out here?

>Here's my problem.

>I  have created a  macro testmac(par1,par2,par3) that returns a single
>value. I also have a dataset mydata with some variables v1,v2,v3 .....

>Now I would like to loop thru mydata, invoking testmac each time with the
>variables v1,v2,v3 and store the result as a new variable in the same
>dataset.

>The basic idea should be something like this:

>data mydata;
> set mydata;
>/* invoke macro with variables v1,v2,v3 of  each obs
> newvar=%testmac(v1,v2,v3)
>run;

>after excution of  the code

>proc print data=mydata;
>run;

>should now give

>v1   v2   v3   newvar other variables
>98     58       68    1    .......
>75     100    83     4    ........
>.....    ...  ..    ... .. ............

>Any help would be appreciated.

>Paul

Guys thank you fro your quick response:

Here's the macro and 4 separate invocation.

147  /* count the number of odd numbers in a sequence */
148    %macro testmac(v1,v2,v3);
149    %let odd=0;
150    %do  i=1 %to 3;
151     %let c = %EVAL(&&v&i / 2);
152     %if  %EVAL(&c * 2) ne &&v&i %then
153         %let odd=%eval(&odd+1);
154      %else
155         %let odd=&odd;
156    %end;
157    %put The number of odd numbers is &odd ;
158    %mend testmac;
159
160  %testmac(2,4,6);
The number of odd numbers is 0
161  %testmac(1,2,4);
The number of odd numbers is 1
162  %testmac(1,3,4);
The number of odd numbers is 2
163  %testmac(1,3,5);
The number of odd numbers is 3

So it correctly counts the number of odd numbers
in the argument sequence v1,v2,v3.

Now I have modified
157 and 158 as follows:

157 &odd
158 mend;

So that it should return just the &odd which I would then like to
assingn in a data step.

For example if a create a data set as follows

data mydata;
 input v1 v2 v3;
 cards;
 2 4 6
 1 2 4
 1 3 4
 1 3 5
;
run;

I would then like to be able to write the following code

data mydata;
 set mydata;
  numodd=%testmac(v1,v2,v3);
run;

and get a new data set with the new varable numodd.
So
proc print data=mydata;
run;

would then give

v1 v2 v3 numodd
2   4  6   0
1   2  4   1
1   3  4   2
1   3  5   3

My problem is I don't what is the proper way to invoke the macro using
the record variables as arguments.

Thanks

Paul

--

Diese E-Mail enth?lt vertrauliche und/oder rechtlich geschtzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtmlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

 
 
 

A question about macro invocation within data step

Post by Pau » Wed, 04 Dec 2002 02:01:01


Harry, Peter and the other guys:

The reasons I want to use macros are:

1. I am trying to learn SAS macro processing.
2. I would like to carry out several different processes on
   some of the variables in each obs, finding for the number of
   odd number is just one of them, hence I think having these
   processes package as a macro would be nice and modular.
3. I have already code and test several of these processes
   as small macros that I have place in the autocall directory.
4. In general I want know the limits of SAS macro processing.

Thanks to all of you for your help.

Paul

 
 
 

A question about macro invocation within data step

Post by Ed Heat » Wed, 04 Dec 2002 06:09:51


Paul,

This is not an analysis of your macro but rather an alternative solution.

%macro testMac( v1 , v2 , v3 ) ;
   %let odd = 0 ;
   %do  i=1 %to 3 ;
      %Let odd = &odd + mod(&&v&i,2) ;
   %end ;
   &odd
%mEnd testMac ;

Data MyData ;
   Input v1 v2 v3 ;
   NumOdd = %testMac( v1 , v2 , v3 ) ;
Cards4 ;
 2 4 6
 1 2 4
 1 3 4
 1 3 5
;;;;

Ed

Edward Heaton, Senior Systems Analyst,
Westat (An Employee-Owned Research Corporation),
1550 Research Boulevard, Room 2018, Rockville, MD 20850-3195
Voice:  (301) 610-4818                 Fax:  (301) 294-3992

-----Original Message-----

Sent: Monday, December 02, 2002 10:24 AM

Subject: Re: A question about macro invocation within data step


>To the SASers:

>Could someone please help me out here?

>Here's my problem.

>I  have created a  macro testmac(par1,par2,par3) that returns a single
>value. I also have a dataset mydata with some variables v1,v2,v3 .....

>Now I would like to loop thru mydata, invoking testmac each time with the
>variables v1,v2,v3 and store the result as a new variable in the same
>dataset.

>The basic idea should be something like this:

>data mydata;
> set mydata;
>/* invoke macro with variables v1,v2,v3 of  each obs
> newvar=%testmac(v1,v2,v3)
>run;

>after excution of  the code

>proc print data=mydata;
>run;

>should now give

>v1   v2   v3   newvar other variables
>98     58       68    1    .......
>75     100    83     4    ........
>.....    ...  ..    ... .. ............

>Any help would be appreciated.

>Paul

Guys thank you fro your quick response:

Here's the macro and 4 separate invocation.

147  /* count the number of odd numbers in a sequence */
148    %macro testmac(v1,v2,v3);
149    %let odd=0;
150    %do  i=1 %to 3;
151     %let c = %EVAL(&&v&i / 2);
152     %if  %EVAL(&c * 2) ne &&v&i %then
153         %let odd=%eval(&odd+1);
154      %else
155         %let odd=&odd;
156    %end;
157    %put The number of odd numbers is &odd ;
158    %mend testmac;
159
160  %testmac(2,4,6);
The number of odd numbers is 0
161  %testmac(1,2,4);
The number of odd numbers is 1
162  %testmac(1,3,4);
The number of odd numbers is 2
163  %testmac(1,3,5);
The number of odd numbers is 3

So it correctly counts the number of odd numbers
in the argument sequence v1,v2,v3.

Now I have modified
157 and 158 as follows:

157 &odd
158 mend;

So that it should return just the &odd which I would then like to
assingn in a data step.

For example if a create a data set as follows

data mydata;
 input v1 v2 v3;
 cards;
 2 4 6
 1 2 4
 1 3 4
 1 3 5
;
run;

I would then like to be able to write the following code

data mydata;
 set mydata;
  numodd=%testmac(v1,v2,v3);
run;

and get a new data set with the new varable numodd.
So
proc print data=mydata;
run;

would then give

v1 v2 v3 numodd
2   4  6   0
1   2  4   1
1   3  4   2
1   3  5   3

My problem is I don't what is the proper way to invoke the macro using
the record variables as arguments.

Thanks

Paul

 
 
 

A question about macro invocation within data step

Post by Jim Groeneve » Wed, 04 Dec 2002 19:08:40


Hi Ed,

Your construct below works, though I would prefer a CALL EXECUTE / SYMGET
construct to let the whole macro run at data step run time. There may be
many macros that won't work correctly this way. The reason that your simple
construct works is that during macro run and resolution the data step code
for NumOdd after resolution of &odd becomes:

NumOdd = 0 + mod(v1,2) + mod(v2,2) + mod(v3,2);

which is a legal assignment, executed at data step run time and directly
containing the input data.

Regards - Jim.
--
Y. (Jim) Groeneveld, MSc    IMRO TRAMARKO         tel. +31 412 407 070
senior statist./data man.   P.O. Box 1            fax. +31 412 407 080

My computer does what I tell it to do; sometimes it says my instructions are
illegal.

Notice of confidentiality: this e-mail may contain confidential information
intended for the addressed recipient only.
If you have received this e-mail in error please delete this e-mail and
please notify the sender so that proper delivery can be arranged.

> -----Original Message-----

> Sent: 02 December 2002 22:10

> Subject: Re: A question about macro invocation within data step

> Paul,

> This is not an analysis of your macro but rather an
> alternative solution.

> %macro testMac( v1 , v2 , v3 ) ;
>    %let odd = 0 ;
>    %do  i=1 %to 3 ;
>       %Let odd = &odd + mod(&&v&i,2) ;
>    %end ;
>    &odd
> %mEnd testMac ;

> Data MyData ;
>    Input v1 v2 v3 ;
>    NumOdd = %testMac( v1 , v2 , v3 ) ;
> Cards4 ;
>  2 4 6
>  1 2 4
>  1 3 4
>  1 3 5
> ;;;;

> Ed

> Edward Heaton, Senior Systems Analyst,
> Westat (An Employee-Owned Research Corporation),
> 1550 Research Boulevard, Room 2018, Rockville, MD 20850-3195
> Voice:  (301) 610-4818                 Fax:  (301) 294-3992


[.........]
 
 
 

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. New HyperAccess Beta?

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

4. Limit to Dictionary Values?

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

6. Icons - I want to make them as useful as ProSel

7. SYSPARM: MACRO v Data Step question

8. logon problem from windows 98

9. Concatenate within a data step

10. Calling a Java Application from SAS within a data step and returning to SAS

11. Calling a Java Application from SAS within a data step andreturning to SAS

12. Question on usage of data conversion functions in a Data Step ;

13. Question on usage of data conversion functions in a Data Step;