Merge 2 variables

Reply
Contributor
Posts: 33

Merge 2 variables

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

Super Contributor
Posts: 284

Re: Merge 2 variables

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.

Contributor
Posts: 33

Re: Merge 2 variables

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

 

 

Super Contributor
Posts: 284

Re: Merge 2 variables

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');

 

 

Super Contributor
Posts: 284

Re: Merge 2 variables

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.

Contributor
Posts: 33

Re: Merge 2 variables

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

Super Contributor
Posts: 284

Re: Merge 2 variables

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;

 

Respected Advisor
Posts: 4,976

Re: Merge 2 variables

Isn't this simply:

 

if number = ' ' then number = work;

Ask a Question
Discussion stats
  • 7 replies
  • 184 views
  • 1 like
  • 3 in conversation