BookmarkSubscribeRSS Feed
SASSLICK001
Obsidian | Level 7

%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!

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

Please try

%let param= x (a);

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

%put &z;

Thanks,

Jag

Thanks,
Jag
SASSLICK001
Obsidian | Level 7

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

SASSLICK001
Obsidian | Level 7

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

Astounding
PROC Star

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,.

SASSLICK001
Obsidian | Level 7

Yes param has double quotes!

SASSLICK001
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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));

Astounding
PROC Star

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.

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1140 views
  • 1 like
  • 5 in conversation