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.

 

BASUG is hosting free webinars ! Check out recordings of our past webinars: https://www.basug.org/videos. Save the date for our in person SAS Blowout on Oct 18 in Cambridge, MA. Registration opens in September.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1328 views
  • 0 likes
  • 5 in conversation