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
data Want;
set Have;
If VARIABLE = scan("&vlist",3,",") then delete;
run;
Ok, I managed to do it actually.
Sorry for the post 😕
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.
data Want;
set Have;
If VARIABLE = scan("&vlist",3,",") then delete;
run;
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.