BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mathis1
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data Want;
set Have;
If VARIABLE = scan("&vlist",3,",") then delete;
run;
--
Paige Miller

View solution in original post

5 REPLIES 5
Mathis1
Quartz | Level 8

Ok, I managed to do it actually.

Sorry for the post 😕

Shmuel
Garnet | Level 18

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.

PaigeMiller
Diamond | Level 26
data Want;
set Have;
If VARIABLE = scan("&vlist",3,",") then delete;
run;
--
Paige Miller
Tom
Super User Tom
Super User

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;
Quentin
Super User

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.

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1497 views
  • 0 likes
  • 5 in conversation