BookmarkSubscribeRSS Feed
avepo
Fluorite | Level 6

Hi ,

I want to subset a dataset either using IF or Where statement.

But I have multiple values such that it will be difficult to type them all out.

Example,

 

Data want;

set have;

if  country in ( "a-z"); (not sure if I can use where instead of if here)

run;

 

How do I write a syntax without typing a,b,c,.....z (character values for variable country)?

 

Thanks

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can reverse the logic here and compress out values like alphabetic easily, then see if the length is still > 0, e.g.

data want;
  set have;
  if lengthn(compress(country," ","a"))=0;
run;

So, take all the alphabetic characters out of country, then if the length is zero you know it was only those.

Astounding
PROC Star

It doesn't matter if you use IF vs. WHERE.  Either way "a-z" is not something that SAS will interpret correctly. 

 

Where is the list of acceptable country names right now?  On a piece of paper?  In your head?  In another data set?

 

Will the data contain all lowercase letters?  Initial caps?  Upper case only?  Will it vary from data set to data set?

 

Those are the questions you need to address before starting to write a program.

art297
Opal | Level 21

Here are examples using both if and where:

data have;
  input country $;
  cards;
US
Mexico
Canada
1st nation of tribes
;

data want;
  set have;
  if  rank(upcase(first(country))) in ( 65:90);
run;

data want;
  set have (where=(rank(upcase(first(country))) in ( 65:90)));
run;

Art, CEO, AnalystFinder.com

 

Ksharp
Super User

@art297  NOTALPHA() is good .

data have;
  input country $40.;
  cards;
US
Mexico
Canada
1st nation of tribes
;

data want;
  set have;
  if notalpha(country)=1;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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