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

Hi all,

I am working with sas and I have a question. I will try to explain the question properly:

I really appreciate your help and suggestions.

There is a variable in my table that have 5 phases for each observation. Some of the observations such as OBS 1 covers all 5 phases, however some of them do not. For instance obs 2 does not cover phases 2 and 4. I want to alter the table and create the phases( all 5 phrases) for all obs with the value of 0.

Current tabel                                                       wanted table

obs       Var          value                           obs       Var     value

1             1             10                             1             1          10

1             2             20                              1            2           20

1             3             12                              1            3           12

1             4             11                              1            4           11

1             5             10                              1            5           10

 

2             1             12                              2           1             12

2             3             23                              2           2              0

2             5             23                              2           3              23

                                                                2           4              0

                                                                2           5             23

 

3             1             21                              3          1                21

3             3              11                             3           2                0

3            4              50                             3           3                11

                                                                3           4              50

                                                                3           5               0

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input obs       Var          value  ;
cards; 
1             1             10       
1             2             20            
1             3             12        
1             4             11        
1             5             10         
2             1             12                       
2             3             23                       
2             5             23              
3             1             21                    
3             3              11             
3            4              50  
;
run;
proc sql;
select a.*,coalesce(b.value,0) as value
 from ( 
  select *
   from (select distinct obs from have),(select distinct var from have))  as a
left join have as b
 on a.obs=b.obs and a.var=b.var ;
quit;

View solution in original post

2 REPLIES 2
Ksharp
Super User
data have;
input obs       Var          value  ;
cards; 
1             1             10       
1             2             20            
1             3             12        
1             4             11        
1             5             10         
2             1             12                       
2             3             23                       
2             5             23              
3             1             21                    
3             3              11             
3            4              50  
;
run;
proc sql;
select a.*,coalesce(b.value,0) as value
 from ( 
  select *
   from (select distinct obs from have),(select distinct var from have))  as a
left join have as b
 on a.obs=b.obs and a.var=b.var ;
quit;
fama
Fluorite | Level 6
Thanks a lot Ksharp,
The code worked well, although I need to figure out how it works 🙂

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
  • 2 replies
  • 841 views
  • 0 likes
  • 2 in conversation