BookmarkSubscribeRSS Feed
forumsguy
Fluorite | Level 6

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

3 REPLIES 3
BrunoMueller
SAS Super FREQ

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

chang_y_chung_hotmail_com
Obsidian | Level 7

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***
--*;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2112 views
  • 0 likes
  • 4 in conversation