%let param= "x (a)"; %put ¶m; I need to extract x, how can I achieve this? %let z= %scan(¶m,1,'(') ; %put &z; the above code is not working!
Please try
%let param= x (a);
%let z=%scan(¶m,1,' ');
%put &z;
Thanks,
Jag
There are quotation marks for param "x (a)"
Lets put this way
%let param= "x y (a)";
I need to extract x y
%let z= %scan(¶m,1,'(') ;
I am using '(' as delimiter but I think I have to %UNQUOTE, %BQUOTE etc to work properly
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(¶m, " ");
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(¶m, "(" );
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,.
Yes param has double quotes!
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
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(¶m));
So this version will do it:
%let z = %scan(¶m, " ");
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.
Very interesting.
%let param="x (a)" ;
%let param_clean = %scan(¶m,1,,ka);
%put ¶m ;
%put ¶m_clean ;
57
58 %let param="x (a)" ;
59 %let param_clean = %scan(¶m,1,,ka);
60
61 %put ¶m ;
"x (a)"
62 %put ¶m_clean ;
x
63
64 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
76
Xia Keshan
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.