SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
beginner
Calcite | Level 5

Hi, I'd like to use "wildcards" in an if/then statement, and am wondering if I'm correct in thinking the "%" sign is the wildcard for any character (including numbers that are "type"-ed as "character" and not "numeric" entries).  In particular, I'm trying to do the following.  I have a dataset A, that looks something like this:

patient          blah          other variables

Joe              aaaa21xy          .....

Bob              aaaa34zq          .....

Tom              rsbq76zv          .....

 

From which I want to create a dataset B, containing all patients for which the "blah" variable begins with the characters "aaaa" (and can have any characters after this).  In othe words, dataset B would looks like this:

patient          blah          other variables

Joe              aaaa21xy          .....

Bob              aaaa34zq          .....

 

I tried & failed to do this with the following if/then statement:

data datasetB; set datasetA;
if blah='aaaa%%%%' then output;
run;

 

I've also tried to use "____" or ":", instead of the "%%%%", and these didn't work either (outputs are all empty datasets).  Thanks in advance for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
slchen
Lapis Lazuli | Level 10

There are some great differences between if and where .  Here is one case.

where blah like 'aaaa%';

or

if blah=:'aaaa';

 

 

View solution in original post

3 REPLIES 3
slchen
Lapis Lazuli | Level 10

There are some great differences between if and where .  Here is one case.

where blah like 'aaaa%';

or

if blah=:'aaaa';

 

 

beginner
Calcite | Level 5

Thank you!

Astounding
PROC Star

 A couple of additional notes that might be useful ...

 

=: does not mean "begins with".  It means make the comparison based on whichever string has the shorter length.  Ignore any characters beyond that length in the longer string.  So these comparisons are identical:

 

if blah =: 'aaaaa';

if 'aaaaa' =: blah;

 

Also, you can use =: with the IN operator, with strings of different length:

 

if blah in ('aaaaa', 'abc', 'bb');

 

Good luck.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 4846 views
  • 1 like
  • 3 in conversation