- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have a list of variable. When running %scan((&vlist.),3,","); I obtain the character string "Los Angeles" but not inside "". Now I want to delete the observations when my character variable VAR takes the value "Los Angeles".
Basically, i'm looking to run this code, but precising that the result of the %scan() function is a character string :
data Want;
set Have;
If VARIABLE = %scan((&vlist.),3,",") then delete;
run;
For information, when running :
data Want;
set Have;
If VARIABLE = "Los Angeles" then delete;
run;
I get the result that I want.
Thank you for your help
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data Want;
set Have;
If VARIABLE = scan("&vlist",3,",") then delete;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ok, I managed to do it actually.
Sorry for the post 😕
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Why are you using %scan, if your code is not part of a macro program?
What does &vlist contain?
I seems to me that you may want use one of next functions:
index() or find() instead %scan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data Want;
set Have;
If VARIABLE = scan("&vlist",3,",") then delete;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Get the SAS code working before introducing macro variables and/or macro logic.
So start with your first program that just has one value as constant text. Now write SAS code that deals with parsing one value from a longer string. For that you would use the SCAN() function (not the macro function %SCAN()).
If VARIABLE = scan("Los Angeles,New York",1,',') then delete;
Now if you have a macro variable VIST with that text. Like:
%let vist=Los Angeles,New York;
You can use it in place of the literal text. Make sure to use double quotes and not single quotes.
If VARIABLE = scan("&visit",1,',') then delete;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
If you have a macro variable holding a comma delimited list like:
%let vlist=Los Angeles,New York,Providence;
you can use %SCAN inside of double quotes, no problem. The only trick is you need to add macro quoting, to mask the commas.
If VARIABLE = "%scan(%bquote(&vlist),1,%str(,))" then delete;
I think that's a reasonable use of the macro language. It could even execute faster that changing to use the scan function, depending on whether or not the data step compiler is smart enough to realize that something like:
scan("Los Angeles,New York,Providence",1,',')
only needs to execute scan once.
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.