DATA Step, Macro, Functions and more

Creating a subset by selecting a specific character string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Creating a subset by selecting a specific character string

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


Accepted Solutions
Solution
‎09-06-2017 11:30 PM
PROC Star
Posts: 765

Re: Creating a subset by selecting a specific character string

Posted in reply to FoxMulder

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


All Replies
Super Contributor
Posts: 340

Re: Creating a subset by selecting a specific character string

Posted in reply to FoxMulder

You have to rectify your input statement:

 

input name $ 1-30;
Solution
‎09-06-2017 11:30 PM
PROC Star
Posts: 765

Re: Creating a subset by selecting a specific character string

Posted in reply to FoxMulder

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;
Super User
Posts: 10,044

Re: Creating a subset by selecting a specific character string

Posted in reply to FoxMulder
SCAN() can pick up the last name.

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


☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 118 views
  • 3 likes
  • 4 in conversation