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 ...............
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!
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.
Ready to level-up your skills? Choose your own adventure.