BookmarkSubscribeRSS Feed
user24
Obsidian | Level 7

How can i merge 2 variable if the one ofthem is null

 

id    work   number

5      0       

4                 

8      1         1-2

9      2         2-5

 

i want to change as if the number is null merge with 'work'

 

id    work   number

5      0         0

4                 

8      1         1-2

9      2         2-5

7 REPLIES 7
collinelliot
Barite | Level 11

Do you have any code that you're currently trying? It's not clear to me if you want to merge on all three variables or just the first two and you want to figure out how to deal with third.

user24
Obsidian | Level 7

if the 'number' is null and 'work' has variable, add that variable to the 'number' by 'id'. Is that clear? 

 

 

collinelliot
Barite | Level 11

Not entirely, but I can be a bit daft. But from what I understand, you could try:

 

if not missing(work) then number = coalescec(number, '0');

 

 

collinelliot
Barite | Level 11

Or if you mean that you want to populate 'number' with 'work,' you could do:

 

number = coalescec(number, work);

 

I'm not sure about what the variable types are, though.

user24
Obsidian | Level 7

I have chracter variable. Also you can think like its left join . i only want to take if the work has variable and number doesn;t have variable.

 

id    work   number

11    2        2

12  

13    3       3

15   1       1-2

16    2       2-3 

 

So i have the table without red variables. I want the table with red variables

collinelliot
Barite | Level 11

Like this?

 

data have;
infile datalines dsd;
input id work $ number $;
datalines;
11, 2,       
12, ,   
13, 3,
15, 1, 1-2
16, 2, 2-3 
;

data want;
    set have;
    number = coalescec(number, work);
run;

 

Astounding
PROC Star

Isn't this simply:

 

if number = ' ' then number = work;

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
  • 7 replies
  • 1529 views
  • 1 like
  • 3 in conversation