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 ...............


sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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