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

Hi all,

 

I am new using SAS, so probably this is a simple question, but I am stuck. I need to create a subset of a file by selecting observations for a specific name that contains a specific character string, let's say 'Smith'. for example, the data is like this:

data test;
   input name $;
   datalines;

Will, Smith
Diana, Ross
Diane, Keaton
Sally, Field
Willy, Thomas
David, Platt
Ana, Smith

;

run;

 

My code is like this:

data test;
set test;
      where name contains 'Smith';
run;

proc print;
run;

 

The code works properly for names, like 'David', but I am interested in  last names, like Smith. Unfortunately it is not working for last names.

Any advice is welcome.

 

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Keep an eye on the length of your character variable 'name', like this

 

data test;
   input name $20.;
   datalines;
Will, Smith
Diana, Ross
Diane, Keaton
Sally, Field
Willy, Thomas
David, Platt
Ana, Smith
;

data test;
set test;
      where name contains 'Smith';
run;

proc print;
run;

View solution in original post

3 REPLIES 3
user24feb
Barite | Level 11

You have to rectify your input statement:

 

input name $ 1-30;
PeterClemmensen
Tourmaline | Level 20

Keep an eye on the length of your character variable 'name', like this

 

data test;
   input name $20.;
   datalines;
Will, Smith
Diana, Ross
Diane, Keaton
Sally, Field
Willy, Thomas
David, Platt
Ana, Smith
;

data test;
set test;
      where name contains 'Smith';
run;

proc print;
run;
Ksharp
Super User
SCAN() can pick up the last name.

if scan(name,-1)='Smith' then ...............


hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4826 views
  • 3 likes
  • 4 in conversation