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

In SAS can we do something like

 

proc sql; 

    select a.*, b.new_var

    from data1 as a

    left join data2 as b

      on a.subjid=b.subjd; 

quit; 

 

but b.new_var is created if an observation from a has missing(variable1). If missing(a.variable1), then read b.variable2, b.variable3, b.variable4 and create a new_var based on some direction involving variable2 and variable3 and variable4. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@gsk wrote:

In SAS can we do something like

 

but b.new_var is created if an observation from a has missing(variable1). If missing(a.variable1), then read b.variable2, b.variable3, b.variable4 and create a new_var based on some direction involving variable2 and variable3 and variable4. 


For example, something like this

 

proc sql; 
    select a.*, 
      case when missing(variable1) then b.variable2+b.variable3+b.variable4
            end else . as new_var,
    from data1 as a
    left join data2 as b
      on a.subjid=b.subjd; 
quit; 

 

 

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@gsk wrote:

In SAS can we do something like

 

but b.new_var is created if an observation from a has missing(variable1). If missing(a.variable1), then read b.variable2, b.variable3, b.variable4 and create a new_var based on some direction involving variable2 and variable3 and variable4. 


For example, something like this

 

proc sql; 
    select a.*, 
      case when missing(variable1) then b.variable2+b.variable3+b.variable4
            end else . as new_var,
    from data1 as a
    left join data2 as b
      on a.subjid=b.subjd; 
quit; 

 

 

 

--
Paige Miller
s_lassen
Meteorite | Level 14

Just use the coalesce function:

proc sql; 
    select data1.*, coalesce(data2.new_var,data2.var2*data2.var3,data2.var4)
    from data1 
    left join data2
      on data1.subjid=data2.subjd; 
quit; 

In the case shown, if new_var is missing, the step will use var2*var3, and if that is also missing, var4, in this case all from data2 (but you could also use variables from the data1 table).

 

And for God's sake: Stop using stupid and non-informative aliases like "a" and "b" in PROC SQL. It may save you a few keystrokes in the beginning, but in the long run it makes the code totally unreadable!

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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