DATA Step, Macro, Functions and more

Create global macro variable inside a macro

Reply
Frequent Contributor
Posts: 85

Create global macro variable inside a macro

Hi Guys,

I am trying to create the macro variables using a macro to use it in my code further but it seems those r restricted only to that macro... Can somebody help me with this

%MACRO M1(COL,TAB,MACROVAR);

PROC SQL NOPRINT;

CREATE &COL INTO :MACROVAR FROM &TAB;

QUIT;

%MEND;

%M1(MSGID,SASHELP.ADOMSG,A1);

%PUT &A1;

This gives me error ... is there any alternative to this ????

Respected Advisor
Posts: 3,887

Re: Create global macro variable inside a macro

SAS Super FREQ
Posts: 682

Re: Create global macro variable inside a macro

hi

Find below a working example.

Macro variables created within a macro have usually a LOCAL scope. However you can make macro variables global, see example below

I have also made some changes to your code sample

%MACRO M1(COL,TAB,MACROVAR);
  %
* make the macro var global;
 
%global &macrovar;

  %
*
   * we are reading only the first obs of the table
   * the TRIMMED option will take away trailing blanks
   *;

  PROC SQL NOPRINT;
    select
      &col
      into
      :&macrovar TRIMMED
    from
      &tab(obs=
1)
    ;
  quit;
 
%put _local_;
%MEND;

%
M1(MNEMONIC,SASHELP.ADOMSG,A1)
%PUT NOTE: *&A1*;

From your code it looks like you want to be able to access some messages from a data sets. Please have a look at the

SASMSG http://support.sas.com/documentation/cdl/en/nlsref/63072/HTML/default/viewer.htm#n0pbh06faefiuyn1pae...

and

SASMSGL http://support.sas.com/documentation/cdl/en/nlsref/63072/HTML/default/viewer.htm#n1iekis5qclbspn1wd6...

functions as well

Regular Contributor
Posts: 241

Re: Create global macro variable inside a macro

I like Bruno's sasmsg function idea a lot. Here is an example I made up. Hopefully the function is not opening-and-closing the message dataset once for each call, but I am not sure. Does anybody know for sure? Bruno?

/* load the data into a sas message dataset */
proc sql;
  create table heights as
  select "en_US" as locale length=5
       , trimn(upcase(name)) as 'KEY'n length=60
       , 1 as lineno length=5
       , strip(putn(height, "best8.")) as text length=1200
  from sashelp.class
  order by locale, 'KEY'n, lineno desc;
  create index indx on heights(locale, key);
quit;
 
%*-- usage example --*;
options locale="en_US";
%put Alfred:***%sysfunc(sasmsg(heights, ALFRED, noquote))***;
%put Alice :***%sysfunc(sasmsg(heights, ALICE, noquote))***;
%*-- on log
Alfred:***69***
Alice :***56.5***
--*;

Ask a Question
Discussion stats
  • 3 replies
  • 290 views
  • 0 likes
  • 4 in conversation