SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I've one macro macro variable which is resolves to ''''''0001'''''' as you see in log below. I'm not using any single quote or double quotes while calling that macro variable. Can someone guide me to tackle this siutation by replacing multiple quotes with single quote? If I do so, then I believe this error can be elimninated.

 

ERROR: All variables in array list must be the same type, i.e., all numeric or character.
MPRINT(TEST):   if scan(line, 5, " ", 'm') not in (''''''0001'''''') then error = error * 45;
NOTE: Line generated by the macro function "SYSFUNC".
4157       ''''''0001''''''
9 REPLIES 9
yabwon
Onyx | Level 15

How the macro variable is created?

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

If possible resolve the issue at the source. How do you create and populate this macro var?

 

But here you go

%let var='''''''abc''''''';
%let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
%put &=var;
David_Billa
Rhodochrosite | Level 12

@Patrick @yabwon I'm creating the macro variable as you see below.

 

%let id=0013;
%let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),',')); 
%put &id.;

and I'm calling it like &id.

yabwon
Onyx | Level 15

If you run it like that how do you get those multiple quotes?

My log is:

1    %let id=0013;
2    %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
3    %put &id.;
'0013'
4    %let id=0013,0017;
5    %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
6    %put &id.;
'0013','0017'
7    %let id=0013,0017,0042;
8    %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
9    %put &id.;
'0013','0017','0042'

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User

@David_Billa wrote:

@Patrick @yabwon I'm creating the macro variable as you see below.

 

%let id=0013;
%let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),',')); 
%put &id.;

and I'm calling it like &id.


That places one set of single quotes around the value. What are you doing that manages to place multiple sets? Are you calling the

%let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));

portion multiple times without resetting ID to the base value?

Example:

1    %let id=0013;
2    %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
3    %put &id.;
'0013'
4    %let id=0013;
5    %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
6    %put First time: &id.;
First time: '0013'
7
8    %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
NOTE: Line generated by the macro function "SYSFUNC".
1    ''0013''
     --
     49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
             release.  Inserting white space between a quoted string and the succeeding
             identifier is recommended.

9    %put Second time: &id.;
NOTE: Line generated by the macro variable "ID".
1     ''0013''
      --
      49
Second time: ''0013''
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
             release.  Inserting white space between a quoted string and the succeeding
             identifier is recommended.

10
11   %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),','));
12   %put Third time: &id.;
Third time: '''0013'''

So it appears that you have a loop that is calling your %let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),',')); multiple times.

Don't do that and you extra quotes likely go away.

Or reset the initial value.

 

 

Tom
Super User Tom
Super User

@David_Billa wrote:

@Patrick @yabwon I'm creating the macro variable as you see below.

 

%let id=0013;
%let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),',')); 
%put &id.;

and I'm calling it like &id.


First thing to do is not overwrite your input macro variable with the modified value.

%let list=0013,0014;
%let qlist=%sysfunc(tranwrd(%bquote('&list'),%str(,),','));

If you really feel a need to overwrite the unquoted variable with the quoted version the first remove any quotes.

2334  %let id='0013';
2335  %let id=%sysfunc(tranwrd(%bquote('%qsysfunc(dequote(&id))'),%str(,),','));
2336  %put &=id;
ID='0013'

Now you won't end up with strings like '''0013'''.

David_Billa
Rhodochrosite | Level 12

@Patrick I tried your code with different values and with different number of quotes and the result is,

 

26         %let var='''''''1234''''''';
27         %let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
28         %put &=var;
VAR=1234
29         
30         
31         %let var='''''0401''''';
32         %let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
33         %put &=var;
VAR=0401
34         
35         
36         %let var=''5678'';
                    __
                    49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release.  Inserting white space 
             between a quoted string and the succeeding identifier is recommended.

37         %let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
38         %put &=var;
VAR=5678
39         
40         %let var='1111';
41         %let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
42         %put &=var;
VAR=1111

Program which I ran is,

 

%let var='''''''1234''''''';
%let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
%put &=var;


%let var='''''0401''''';
%let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
%put &=var;


%let var=''5678'';
%let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
%put &=var;

%let var='1111';
%let var=%sysfunc(prxchange(s/%nrbquote(')+//,-1,%nrbquote(&var)));
%put &=var;

Desired result is,

 

VAR='1234'
VAR='0401'
VAR='5678'
VAR='1111'
Patrick
Opal | Level 21

Use a data step instead. That's easier to read and "safer".  ....but much better fix the issue at the source so that you never end-up with that many quotes.

%let var=%nrbquote(''5678'');
data _null_;
  var=symget("var");
  var=compress(var,"'");
  call symputx('var',cats("'",var,"'"));
  stop;
run;
%put %nrbquote(&var);
Kurt_Bremser
Super User

You are doing something in the macro that adds the quotes; they don't appear magically out of nothing:

%let id=0013;
%let id=%sysfunc(tranwrd(%bquote('&id.'),%str(,),',')); 
%put &id.;

%macro check(in);
data check;
x1 = &id.;
run;
%mend;

%check(&id.)

BTW, in about 99% of cases, quotes in macro variables are not needed and cause only unnecessary work. I suggest you review your code design.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 9 replies
  • 3117 views
  • 3 likes
  • 6 in conversation