sas >> Pass a variable as a macro parameter.

by js8765 » Mon, 08 Mar 2010 20:56:14 GMT

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

sas >> Pass a variable as a macro parameter.

by jim.1stat » Mon, 08 Mar 2010 21:44:32 GMT


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.

sas >> Pass a variable as a macro parameter.

by gerhard.hellriegel » Mon, 08 Mar 2010 22:27:27 GMT

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

sas >> Pass a variable as a macro parameter.

by Tom Abernathy » Tue, 09 Mar 2010 11:47:26 GMT

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||')');



> %let strInput2 = &strInput &strInput;> > &strInpu>2
> %men>; >>
> Now, I'd like to use this macro in a data _null_ step and pass a stri>g
> variable as a parameter e.>. >>
> data _null>;
> myString='This is a stri>g';
> myString2="%myMacro(myStr>ng)";
> put 'myString2 is: ' my>tring2;> >>run;
>
> As expected, the macro processes the string "myString" as oppose> to the
> value of the v>ri>ble.
>
> I thought of using a global variable and "call symput", >ut then
> realised that the global variable just gets assigned its value >hen the
> data step reaches the run st>te>ent.
>
> Does anyone know a good way to do this or is my overall approac> w>ong?
>
> Thanks in advance for any help or info>ma>ion!
>

Similar Threads

1. %label: passing variables rather than literals for &COLOR and &STYLE macro parameters

Hello,

I'm using SAS' %LABEL annotate macro and I would like to set the color and
font used for the label so that it is dependent on the variables F_ALLOWED
and F_STUDY, respectively. I've been attempting to use code such as the
following:

data  ANNOTATE;

   %dclanno;
   %system(2, 2);

   length   FONT
            SYMBOL_COLOR   $8;

   set      FOR_ANNOTATE;

   if       F_STUDY = 1
   then     FONT = "marker";
   else     FONT = "markere";

   if       F_ALLOWED = 0
   then     SYMBOL_COLOR = "red";
   else     SYMBOL_COLOR = "green";

   %LABEL(SDAY_PDT, YVAL, "U", SYMBOL_COLOR, 0, 0, 1.25, FONT, +);

run;

Unfortunately, the %LABEL macro assumes that the &COLOR and &STYLE
parameters are character literals and not variables. Accordingly, the
annotate dataset contains values of "SYMBOL_C" for COLOR and "FONT" for
STYLE, rather than the desired values.

Now, I realize I could just code four %LABEL calls for each combination of
F_STUDY and F_ALLOWED, but I would like to do something slicker with just
one %LABEL call. Does anyone on the list have any suggestions?

Many thanks,

Matt Downs
Statistics Collaborative, Inc.
1710 Rhode Island Ave NW Ste 200
Washington DC 20036

2. quoting portions of macro variables in first pass, unquoting in 2nd pass

3. Put single quotes on a macro parameter passed from Call Execute

Hello,

=20

I am using CALL EXECUTE in a DATA step to call a macro program which
uses SQL pass-through to execute a SQL Server 2005 stored procedure.  I
need to pass two of the data set variables as macro parameters.  The
problem I am having is that the stored procedure has to have these
parameters in SINGLE quotes.  Initially, I used double quotes in the
stored procedure syntax because I needed double quotes so the macro
parameters would resolve - but I got a syntax error from SQL Server.

=20

Now, I am trying to think of how I could add the single quotes to the
data set variables, so they are passed as part of the macro parameter.
Or, could I use a strategy to send the parameter without quotes and
build a string that includes a single quote on each end of the resolved
parameter (%sysfunc(cats("'",&vlab.,"'")) did not work).

=20

I think I need a macro quoting expert!

=20

This is what I have (which isn't working)..

=20

* macro to create captions for SQL Server fields from SAS variable
labels ;

* (uses SQL pass-through facility) ;

*    vname - variable name ;

*    vlab  - variable label ;

%macro putlabs (vname=3D,vlab=3D);

=20

proc sql;

   connect to odbc as mydb (datasrc=3D"wf_test_db");

   execute

       (sys.sp_addextendedproperty

       @name=3DN'Caption'

       ,@value=3DN"&vlab."=20

       ,@level0type=3DN'SCHEMA'

       ,@level0name=3DN'dbo'

       ,@level1type=3DN'TABLE'

       ,@level1name=3DN'pharm07_d'

       ,@level2type=3DN'COLUMN'

       ,@level2name=3DN"&vname."

       by mydb=20

      ;

quit;

=20

%mend putlabs;

=20

* create table of sas data set variables and labels ;=20

proc sql; =20

   create table pharm07_labs as

   select name, label

   from   dictionary.columns

   where  libname =3D "WFD" and

          memname =3D "PHARM07_D"

   ;

quit;

=20

=20

* add captions to SQL Server fields ;

data _null_;

   set pharm07_labs;

call execute('%putlabs(vname=3D'||name||',vlab=3D'||label||')');

run;

=20

=20

The CALL EXECUTE - as written - is properly sending the parameters, but
the stored procedure is crashing because I am using double quotes around
the macro variable names. =20

=20

Thanks in advance.

=20

=20

Jack Clark

Research Analyst

Center for Health Program Development and Management

University of Maryland, Baltimore County

=20

=20

4. Put single quotes on a macro parameter passed from Call

5. Passing a string including % as a macro parameter

%Let Port=AB;

%Macro Dummy(line);
%Put &line;
%Mend;

How can I call the %Dummy macro (or modify it) so that it prints:

"%Let MyPort=AB;"

?

Calling it as %Dummy("%Let MyPort=&Port.;") doesn't work as %Let is
evaluated before the call.

Calling it as %Dummy(%Nrstr("%Let MyPort=&Port.;")) doesn't work as
then &Port is not evaluated.

6. Error in Passing Parameters in Macros (Urgent)

7. New question: passing in parameters to macro (newbie question)

I am sending this again so that people know it is a new question...sorry
about that! Kim

Hi,

I can't seem to figure this out.
I am having a problem with the nk value, I think.
I think that in the rand macro, it doesn't like my line
        %IF (UNIT <= b) %THEN TREAT = 1
The error message is:
A character operand was found in the %EVAL function or %IF condition
where a numeric
       operand is required. The condition was: &BLNUM
ERROR: The %TO value of the %DO UNIT loop is invalid.
ERROR: The macro RAND will stop executing.

If anyone could help me it would be greatly appreciated; the code is
below.

Thanks!
Kim

%macro analysis(numSimulations=,K=, seed=,max_size=,mu=);
%do ns = 1 %to &numSimulations;
  %do k = 1 %to &K;
    data _null_;
      nk=min(&max_size,ranpoi(&seed,&mu));
      call symput("nk",left(put(nk,12.6)));
          stop;
        run;

    %rand(start=0,end=0,blnum=&nk,type=1);
%end;
%end;
%mend;

%macro rand(start=,end=,blnum=,type=);
    DATA A;
     %DO UNIT=1 %TO &blnum;
            b=&blnum/2;
        %IF (UNIT <= b) %THEN TREAT = 1;
        %ELSE                TREAT = 2;
        OUTPUT;
          %END;
        run;
%mend;


%analysis(numSimulations=1,K=1, seed=0,max_size=30,mu=20);

8. passing in parameters to macro (newbie question)