DATA Step, Macro, Functions and more

Deriving Macro variable name from other macro variables

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Deriving Macro variable name from other macro variables


Hi,

I am creating a macro for create table in teradata and i woudl like to call that macro for each table. So i have wrote the below mentioned code but i am getting a typical warning and the table is not created as below

Code:

%let ini= VK;

%let fcode=ab123;

%let work=wt;

%let brand2 = Very;

%let SEASON2=AW14;

%macro filecheck (DB=, Tablename=, sql_code=, indx=);

proc sql;

connect to teradata (user=&user. password=&password. mode=teradata);

OPTIONS COMPRESS=YES;

EXECUTE    (ct &tablename. &sql_code. &indx.) by teradata;

%mend;

  %filecheck_create
(DB=*****, Tablename=
%put(%trim(&ini._&Fcode._brow_&brand2._&season2.)_&work.),

sql_code=(aba, abc), indx= primary index (abc)) by teradata;

WARNING: The quoted string currently being processed has become more than 262 characters long.  You might have unbalanced quotation marks.

Above code resolving the table name but it is not creating the table and generating the warning. Can some one help what is the solution for the same to define table in the same pattern.

Regards,

Venkat P


Accepted Solutions
Solution
‎09-22-2014 08:45 AM
Super User
Super User
Posts: 7,942

Re: Deriving Macro variable name from other macro variables

Posted in reply to venkata_peddisetty

Hi,

I suspect that the this is the problem:

%put(%trim(&ini._&Fcode._brow_&brand2._&season2.)_&work.)

Not sure why you need the %put to start with.  Also the %trim is great, however your not trimming the individual elements:

Use options mlogic mprint symblogen;

You will see that &ini for instance will resolve as the text, plus some spaces (up to minimum length 8 I believe)so your output may look like:

ini=xyxy, bran2=tytyty

xyxy    _brow_tytyty   ...

Try to trim each bit:

%trim(&ini)_%trim(&fcode)_brow_%trim(&brand2)...


AS a final thought, you might want to consider using catx function e.g:

%sysfunc(catx("_",&ini.,&fcode.,"_brow_",&brand2....)

View solution in original post


All Replies
Solution
‎09-22-2014 08:45 AM
Super User
Super User
Posts: 7,942

Re: Deriving Macro variable name from other macro variables

Posted in reply to venkata_peddisetty

Hi,

I suspect that the this is the problem:

%put(%trim(&ini._&Fcode._brow_&brand2._&season2.)_&work.)

Not sure why you need the %put to start with.  Also the %trim is great, however your not trimming the individual elements:

Use options mlogic mprint symblogen;

You will see that &ini for instance will resolve as the text, plus some spaces (up to minimum length 8 I believe)so your output may look like:

ini=xyxy, bran2=tytyty

xyxy    _brow_tytyty   ...

Try to trim each bit:

%trim(&ini)_%trim(&fcode)_brow_%trim(&brand2)...


AS a final thought, you might want to consider using catx function e.g:

%sysfunc(catx("_",&ini.,&fcode.,"_brow_",&brand2....)

Contributor
Posts: 22

Re: Deriving Macro variable name from other macro variables

Hi RW / Jaap,

Well the issue is resolved and thanks for you both for quick reply.

Sorry Jaap while giving example i hgave incorrect Macroname, but in the process i used corret name. The issue with trim function using for all the macro variables, thanks RW.

Regards,

Venkat P

Trusted Advisor
Posts: 3,212

Re: Deriving Macro variable name from other macro variables

Posted in reply to venkata_peddisetty

You are defining a macro named filecheck and than using one filecheck_create.  Getting an error that is most likely caused by unbalanced apostrophes. 

The things you have posted is inconsistent. Restart your eguide/sas-session and before submitting any code, review that carefully on colors indicating something coded badly.

---->-- ja karman --<-----
Super User
Super User
Posts: 7,039

Re: Deriving Macro variable name from other macro variables

Posted in reply to venkata_peddisetty

From your error message it looks like you have unbalanced quotes somewhere in your program.

As to the idea of building up macro variable names (or any other string) from macro variables you just need to reference the variables in the right place.  For example if you want to build the value for the Tablename parameter from all of those other macro variables then just reference them in order.  Tablename=&ini._&Fcode._brow_&brand2._&season2._&work.

If that does not work (say because it is producing spaces in the middle of what should be a table name) then the problem is in the individual macro variables and should be fixed when they are created.

If what you wanted to build was not a table name, but instead the name of a macro variable that would actually hold the name of the table then you could add two extra &'s in front.  When SAS sees two adjacent &'s it resolves them to one & and then re-evaluates the results again for macro variable references.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 326 views
  • 0 likes
  • 4 in conversation