DATA Step, Macro, Functions and more

Macro quoting functions

Reply
Contributor
Posts: 50

Macro quoting functions

%let param= "x (a)"; %put ¶m; I need to extract x, how can I achieve this? %let z= %scan(&param,1,'(') ; %put &z; the above code is not working!

Trusted Advisor
Posts: 1,131

Re: Macro quoting functions

Please try

%let param= x (a);

%let z=%scan(&param,1,' ');

%put &z;

Thanks,

Jag

Thanks,
Jag
Contributor
Posts: 50

Re: Macro quoting functions

There are quotation marks for param "x (a)"

Contributor
Posts: 50

Re: Macro quoting functions

Lets put this way

%let param= "x y (a)"; 

I need to extract x y


%let z= %scan(&param,1,'(') ;


I am using '(' as delimiter but I think I have to %UNQUOTE, %BQUOTE etc to work properly

Super User
Posts: 5,099

Re: Macro quoting functions

First, you have to understand the values you have assigned.  This statement assigns &PARAM a total of 7 characters:

%let param = "x (a)";

If you wanted to assign the 5 characters within the quotes, you would just code:

%let param = x (a);

Given that you used double quotes, x is the second character.  You could extract it using:

%let z = %scan(&param, " ");

But that's actually a trick.  It specifies that %SCAN should use both blanks and double quotes as delimiters.

Given your second test:

%let param = "x y (a)";

This assigns &PARAM a total of 9 characters, where x is the second character.  If you only wanted the characters within quotes, you should have used:

%let param = x y (a);

With &PARAM taking on 9 characters, you could use:

%let z = %scan(&param, "(" );

Again, this is a trick,  it says that %SCAN should use double quotes as well as left-hand parentheses as delimiters.  Repeating the double quote in the second parameter of %SCAN prevents having unbalanced quotes.

So the first question to consider is, did you mean to add the double quotes to the value of &PARAM?

Good luck,.

Contributor
Posts: 50

Re: Macro quoting functions

Yes param has double quotes!

Contributor
Posts: 50

Re: Macro quoting functions

I just gav you the example my data is coming up with the quotations, so I have to remove the quotation marks and then extract the first string

Super User
Super User
Posts: 6,502

Re: Macro quoting functions

You can use the DEQUOTE() function to remove the quotes.  Also has the advantage or de-duping the doubled up embedded quotes and also working fine when the values does not contain quotes.

%let param="x (a)" ;

%let param_clean = %sysfunc(dequote(&param));

Super User
Posts: 5,099

Re: Macro quoting functions

So this version will do it:

%let z = %scan(&param, " ");

The %SCAN function here is using both blanks and double quotes as delimiters.  You don't need to remove the double quotes ... just have %SCAN treat them delimiters.

Super User
Posts: 9,691

Re: Macro quoting functions

Very interesting.

Code: Program

%let param="x (a)" ;
%let param_clean = %scan(&param,1,,ka);

%put &param ;
%put &param_clean ;


Log: Program

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

57 

58 %let param="x (a)" ;

59 %let param_clean = %scan(&param,1,,ka);

60 

61 %put &param ;

"x (a)"

62 %put &param_clean ;

x

63 

64 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

76 

Xia Keshan

Ask a Question
Discussion stats
  • 9 replies
  • 341 views
  • 1 like
  • 5 in conversation