%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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.