From: Jim Groeneveld on
Hi JS,

If you goal is to double a string value using a macro it might be:

%macro myMacro(strInput);
TRIM(&strInput) || TRIM(&strInput) /* no semicolon */
%mend;

data _null_;
LENGTH myString2 $ 32; * reserve enough value space in advance;
myString='This is a string';
myString2=%myMacro(myString);
put 'myString2 is: ' myString2;
run;

Can you use such a construct?

Remember, as you already noticed, that macro code does not process data
values (from a SAS dataset) in a data step. It processes SAS program code
and runs before the SAS code (up to a RUN or QUIT statement) runs.

Regards - Jim.
--
Jim Groeneveld, Netherlands
Statistician, SAS consultant
http://jim.groeneveld.eu.tf


On Mon, 8 Mar 2010 13:56:14 +0100, js8765 <js8765(a)GOOGLEMAIL.COM> wrote:

>Hi there,
>
>I was wondering if anyone could help me with the following.
>
>Imagine I have a simple macro that does some text manipulation e.g.
>
>%macro myMacro(strInput);
> %let strInput2 = &strInput &strInput;
> &strInput2
>%mend;
>
>Now, I'd like to use this macro in a data _null_ step and pass a string
>variable as a parameter e.g.
>
>data _null_;
> myString='This is a string';
> myString2="%myMacro(myString)";
> put 'myString2 is: ' myString2;
>run;
>
>As expected, the macro processes the string "myString" as opposed to the
>value of the variable.
>
>I thought of using a global variable and "call symput", but then
>realised that the global variable just gets assigned its value when the
>data step reaches the run statement.
>
>Does anyone know a good way to do this or is my overall approach wrong?
>
>Thanks in advance for any help or information!
>
>js
From: Gerhard Hellriegel on
you missunderstood the meaning of a macro!
A macro simple creates code and replaces the macro call with that code.
That occures BEFORE the data-step compiler is active. So you cannot pass
any variable content which is created (filled in) in the data-step. What
you could do is passing the name of that variable, which is filled before
the macro code is inserted.
Example:

That makes no sense:

%macro add(v1,v2,result);
&result = sum(&v1,&v2);
%mend;

data _null_;
value1=3;
value2=5;
call symput("v1",value1);
call symput("v2",value2);
%add(&v1,&v2,sum_val);
put sum_val=;
run;

but if you call it like:


data _null_;
value1=3;
value2=5;
%add(value1,value2,sum_val);
put sum_val=;
run;

That works. Reason is, the compiler sees that:

data _null_;
value1=3;
value2=5;
sum_val = sum(value1,value2);
put sum_val=;
run;


So you cannot work with contents of a variable in the same datastep, but
you can work with the names. A check might be to use options mprint; to
see what the macro processor produces and check if that makes any sense
(in the above example only if the syntax is ok, not if that is clever...)

Gerhard




On Mon, 8 Mar 2010 13:56:14 +0100, js8765 <js8765(a)GOOGLEMAIL.COM> wrote:

>Hi there,
>
>I was wondering if anyone could help me with the following.
>
>Imagine I have a simple macro that does some text manipulation e.g.
>
>%macro myMacro(strInput);
> %let strInput2 = &strInput &strInput;
> &strInput2
>%mend;
>
>Now, I'd like to use this macro in a data _null_ step and pass a string
>variable as a parameter e.g.
>
>data _null_;
> myString='This is a string';
> myString2="%myMacro(myString)";
> put 'myString2 is: ' myString2;
>run;
>
>As expected, the macro processes the string "myString" as opposed to the
>value of the variable.
>
>I thought of using a global variable and "call symput", but then
>realised that the global variable just gets assigned its value when the
>data step reaches the run statement.
>
>Does anyone know a good way to do this or is my overall approach wrong?
>
>Thanks in advance for any help or information!
>
>js
From: Tom Abernathy on
As others have said do you really want to operate at the macro
variable level?
Or are you trying to manipulate the values of the dataset variables?

If you really need to run the macro and save the result into the
dataset variable then use the RESOLVE function.

mystring2 = resolve('%mymacro('||mystring||')');


On Mar 8, 7:56 am, js8...(a)GOOGLEMAIL.COM (js8765) wrote:
> Hi there,
>
> I was wondering if anyone could help me with the following.
>
> Imagine I have a simple macro that does some text manipulation e.g.
>
> %macro myMacro(strInput);
>     %let strInput2 = &strInput &strInput;
>     &strInput2
> %mend;
>
> Now, I'd like to use this macro in a data _null_ step and pass a string
> variable as a parameter e.g.
>
> data _null_;
>     myString='This is a string';
>     myString2="%myMacro(myString)";
>     put 'myString2 is: ' myString2;
> run;
>
> As expected, the macro processes the string "myString" as opposed to the
> value of the variable.
>
> I thought of using a global variable and "call symput", but then
> realised that the global variable just gets assigned its value when the
> data step reaches the run statement.
>
> Does anyone know a good way to do this or is my overall approach wrong?
>
> Thanks in advance for any help or information!
>
> js