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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2025 views
  • 1 like
  • 3 in conversation