BookmarkSubscribeRSS Feed
jovane0700
Calcite | Level 5

So I have a .sas7bdat file. I have successfully opened it by using lib name and 

 

data new ;
set roster.employee_roster ;
run;

 

However I am asked to print a table with only peoples last names that start with Z or Y. Im having trouble figuring this out since the names are stored as full names in one column. any suggestions?

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

If the assignment is to only print records with first names starting with Z or X, it shouldn't matter if the name is stored as a full name?

 

Simply do something like this

 

proc print data=sashelp.class;
   where substr(name, 1, 1)="A";
run;

  

jovane0700
Calcite | Level 5

Sorry I rushed to type it. Its asking for LASTNAMES that start with Z or Y. But I only have the column of FULLNAMES 

Reeza
Super User
1. Split names to isolate last name. See SCAN() and/or SUBSTR()
2. Use SUBSTR() to find the first character of name from #1
3. Add an IF statement to filter the records out.

jovane0700
Calcite | Level 5

Do i put these steps in the data step or the proc step? sorry I'm very new to this

mdavidson
Quartz | Level 8
data work.a;
name="Joe Zions";
output;
name="Joe Smith";
output;
name="Joe Yuck";
output;
run;

data work.b;
set work.a;
where substr(upcase(scan(name,2," ")),1,1) in ('Z','Y');
run;
jovane0700
Calcite | Level 5

This is for one column of data. What if I'm working with multiple columns such as a .sas7bdat file?

 

file name: employee_roster.sas7bdat

ballardw
Super User

@jovane0700 wrote:

This is for one column of data. What if I'm working with multiple columns such as a .sas7bdat file?

 

file name: employee_roster.sas7bdat


The technique @mdavidson shows is intended to subset a data set on the values as specified (under some assumptions you have not addressed such as order of first and last name in the name field and some other obnoxious name related stuff). You would use your data set in the place of the work.a on the set statement in the second data step in the example and the work.b set is the one you would print from. His work.a data set is shown to have something concrete to demonstrate one approach. If your name field looks different than shown in work.a then you need to provide examples.

 

Astounding
PROC Star

Here's a good starting point  It assumes (as you originally stated) that the full name is stored in a single variable:

 

proc print data=roster.employee_roster;

where scan(full_name, -1) in : ("Y", "Z");

run;

 

For this to work, you absolutely must not remove the colon after IN.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 1090 views
  • 4 likes
  • 6 in conversation