BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JibJam221
Obsidian | Level 7

Hello everyone, 

 

I have a table that consists of 3 columns: "Name" "FirstName" "LastName".

 

The "name" column is half filled, the other half is blank. 

The "firstname" and "lastname" columns have the other half of the data. I'd like to concatenate these two columns and have the output placed into the "name" column where the values are empty.

 

proc SQL; 
SELECT
  Last_Name || ' ' || First_Name AS Name
FROM work.table1;

run;

this is what I have for the concatenation of the 2 columns, but im not sure how I can write in to have this placed into the NAME column when the value is empty.

 

Any advice is appreciated,

thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    if missing(name) then name=catx(' ',first_name,last_name);
run;
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Please provide a few examples.

--
Paige Miller
JibJam221
Obsidian | Level 7

for sure!

Current Table: 

Fitst Name Last Name Name
    Marsha Jones
John Doe  
Jane Levis  
    Marianne Phil
    Marsha Jones
Mandy Smith  
    Thomas Cruz

 

Desired table:

Name
Marsha Jones
John Doe
Jane Levis
Marianne Phil
Marsha Jones
Mandy Smith
Thomas Cruz

 

So im looking for a code that can concatenate the "first name" and "last name" columns, and insert the values into the pre-existing 'Name' Column, wherever there are blanks.

PaigeMiller
Diamond | Level 26
data want;
    set have;
    if missing(name) then name=catx(' ',first_name,last_name);
run;
--
Paige Miller
Tom
Super User Tom
Super User

Assuming NAME is defined long enough and the pattern of either NAME having a value or the other two having values is consistent then just replace name with combination of all three.

data want;
  set have;
  name=catx(' ',name,firstname,lastname);
run;

If NAME is not long enough then change it s length first.

data want;
  length name $100 ;
  set have;
  name=catx(' ',name,firstname,lastname);
run;
ballardw
Super User

You might try

proc SQL; 
SELECT
  coalescec( name, catx(' ',Last_Name , First_Name))  AS Name
FROM work.table1;

run;

The COALESCEC function, and its partner Coalesce for numeric variables, returns as the result the first value in the list of values provided. So if Name is blank the result of the catx function is returned. I use CATX because it will strip extra spaces from the parameters an place only one space, if needed, between the last and first names.

 


@JibJam221 wrote:

Hello everyone, 

 

I have a table that consists of 3 columns: "Name" "FirstName" "LastName".

 

The "name" column is half filled, the other half is blank. 

The "firstname" and "lastname" columns have the other half of the data. I'd like to concatenate these two columns and have the output placed into the "name" column where the values are empty.

 

proc SQL; 
SELECT
  Last_Name || ' ' || First_Name AS Name
FROM work.table1;

run;

this is what I have for the concatenation of the 2 columns, but im not sure how I can write in to have this placed into the NAME column when the value is empty.

 

Any advice is appreciated,

thanks!

 


 

JibJam221
Obsidian | Level 7

this code concatenates, however the output is into a whole new column instead of the pre-existing name column. the "name" column has some names in it already, and i'd like the first name & last name values to be inserted into the "name" column where there are blanks

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!

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