BookmarkSubscribeRSS Feed
buddha_d
Pyrite | Level 9

Sas Gurus, 

             I am trying to create this data step in proc sql with case when statement and I am getting syntax errors. Any idea how to solve this? Thanks in advance. 

 

DATA NAMES;
INPUT full_name $20.;
if index(full_name, ' ') = 0 then do;
first_name = substr(full_name,1,8);
last_name = substr(full_name,9,9);
end;
else do;
first_name = scan(full_name,1,' ');
last_name = substr(full_name, index(full_name,' '));
end;

CARDS;

;

RUN;

3 REPLIES 3
PGStats
Opal | Level 21

You could do this:

 


proc sql;
create table names as
select
    full_name,
    case when countw(full_name) = 1 then substr(full_name,1,8) else scan(full_name,1) end as first_name,
    case when countw(full_name) = 1 then substr(full_name,9,9) else scan(full_name,2) end as last_name
from have;
quit;
PG
buddha_d
Pyrite | Level 9

Thank you for the suggestion Pgstat. I do have last name that have gaps in last names and that is different when I compare both with proc compare. Is there a way to fix this? 

Thanks a lot.

PGStats
Opal | Level 21

Give an example, please.

PG

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
  • 8161 views
  • 1 like
  • 2 in conversation