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 Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1246 views
  • 0 likes
  • 5 in conversation