0

I am debugging a macro I am writing that will treat a string as either a prefix or suffix of a dataset name based on user input. Then, the quoted result will be fed into another process downstream.

So if the user says Type=1 and provides the string 'data_', the output of the resulting macro variable will be 'data_%'.

I've tried a few different iterations of the below code, but can't quite get it....any help would be greatly appreciated. Thanks.

%let Type=1;
%let TableName=data_;

data _null_;
   if &Type=1 then 
      call symput('qTableName',%unquote(%str(%')(cats(&TableName.,"%"))%str(%')));
   else if &Type=2 then 
      call symput('qTableName',%unquote(%str(%')(cats("%",&TableName.))%str(%')));
run;

%put &qTableName;
andrey_sz
  • 751
  • 1
  • 13
  • 29
pyll
  • 1,688
  • 1
  • 26
  • 44
  • 1
    Since you are using a data step why are you also using all of those macro functions? `call symputx('qtablename',cats(symget('TableName'),'_%'))`. Also are the single quotes part of the value in the generated macro variable or not? – Tom Dec 09 '15 at 18:48

1 Answers1

3

Looks like you are trying to add single quotes to a macro variable.

%let TableName=data_;
%let qTableName='data_%';

So in a data step you can use CATQ().

data _null_;
   call symputx('qTableName',catq('1a',cats(symget('TableName'),'%')));
run;

Or in simple macro code just use %bquote() to allow you to add ' and % without preventing macro variable expansion. But you probably want to remove the macro quoting it will cause.

%let qTableName=%unquote(%bquote('&TableName%'));

Or if you only want to add the % when &TYPE=1 then perhaps you could call the IFC() function. I believe that the %sysfunc() call will remove the macro quoting.

%let Type=1;
%let qTableName=%sysfunc(ifc(&type=1,%bquote('&TableName%'),%bquote('&TableName')));
Tom
  • 47,574
  • 2
  • 16
  • 29
  • Thanks, Tom. The solution you provided in the data step works perfectly for my needs. – pyll Dec 09 '15 at 19:34