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
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;
You have to rectify your input statement:
input name $ 1-30;
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;
SCAN() can pick up the last name. if scan(name,-1)='Smith' then ...............
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.
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.