BookmarkSubscribeRSS Feed
farshidowrang
Quartz | Level 8

Dear programmer friends 

 

I have a dataset according to below. It shows the reservoir depth. For each WELLBORE, the reservoir starts at the minimum TOP_LAYER_DEPTH. For example, the reservoir starts at 5 for WELLBORE A. 

 

WELLBOREDEPTHTOP_LAYER_DEPTH
A0 
A1 
A2 
A3 
A4 
A55
A6 
A7 
A8 
A99
A10 
A11 
A1212
A13 
B0 
B1 
B22
B3 
B4 
B55
B6 
B77
B8 
C0 
C1 
C2 
C3 
C44
C5 
C6 
C7 
C8 
C99
C10 

 

I like to mark where the depth starts for each WELLBORE. I like to create a flag (INSIDE_RESERVOIR_FLG)  to mark this interval. I  want to have such table as below:

 

WELLBORE_NAMEDEPTHTOP_LAYER_DEPTHINSIDE_RESERVOIR_FLG
A0 0
A1 0
A2 0
A3 0
A4 0
A551
A6 1
A7 1
A8 1
A991
A10 1
A11 1
A12121
A13 1
B0 0
B1 0
B221
B3 1
B4 1
B551
B6 1
B771
B8 1
C0 0
C1 0
C2 0
C3 0
C441
C5 1
C6 1
C7 1
C8 1
C991
C10 1

 

 

Can you please help me with it?

 

Thank you very much in advance!

 

Best regards

 

Farshid 

8 REPLIES 8
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by wellbore_name;
    retain flag;
    if first.wellbore_name then flag=0;
    if not missing(top_layer_depth) then flag=1;
run;
    
--
Paige Miller
Kurt_Bremser
Super User

Just a retained variable, and by-group processing:

data want;
set have;
by wellbore;
retain inside_reservoir_flg;
if first.wellbore then inside_reservoir_flg = 0;
if top_layer_depth ne . then inside_reservoir_flg = 1;
run;
farshidowrang
Quartz | Level 8

Dear Friend

 

I don't think it is working well

 

The inside_reservoir_flg shold stop producing "1" when the Wellbore is changing 

 

I don't thin your code do it

 

Best regards

 

Farshid

Kurt_Bremser
Super User

@farshidowrang wrote:

Dear Friend

 

I don't think it is working well

 

The inside_reservoir_flg shold stop producing "1" when the Wellbore is changing 

 

I don't thin your code do it

 

Best regards

 

Farshid


Have you tested it?

farshidowrang
Quartz | Level 8

Thank you very much Kurt!

 

Best regards

 

Farshid 

novinosrin
Tourmaline | Level 20

Hi @farshidowrang  

 


data have;
infile cards expandtabs truncover;
input WELLBORE $	DEPTH	TOP_LAYER_DEPTH;
cards;
A	0	 
A	1	 
A	2	 
A	3	 
A	4	 
A	5	5
A	6	 
A	7	 
A	8	 
A	9	9
A	10	 
A	11	 
A	12	12
A	13	 
B	0	 
B	1	 
B	2	2
B	3	 
B	4	 
B	5	5
B	6	 
B	7	7
B	8	 
C	0	 
C	1	 
C	2	 
C	3	 
C	4	4
C	5	 
C	6	 
C	7	 
C	8	 
C	9	9
C	10	 
;
data want;
 if 0 then set have;
 do INSIDE_RESERVOIR_FLG=0 by 0 until(last.WELLBORE);
  set have;
  by WELLBORE;
  if TOP_LAYER_DEPTH then INSIDE_RESERVOIR_FLG=1;
  output;
 end;
run;
farshidowrang
Quartz | Level 8

WoW!!

 

It works body!!

 

Thank you very much man!

 

Best regards

 

Farshid 

hashman
Ammonite | Level 13

@farshidowrang:

With the availability of the SUM statement, no RETAIN and/or superfluous IF logic are needed:

data have ;                                                                                                                             
  input wellbore $ depth top_layer_depth ;                                                                                              
  cards ;                                                                                                                               
A   0  .                                                                                                                                
A   1  .                                                                                                                                
A   2  .                                                                                                                                
A   3  .                                                                                                                                
A   4  .                                                                                                                                
A   5  5                                                                                                                                
A   6  .                                                                                                                                
A   7  .                                                                                                                                
A   8  .                                                                                                                                
A   9  9                                                                                                                                
A  10  .                                                                                                                                
A  11  .                                                                                                                                
A  12 12                                                                                                                                
A  13  .                                                                                                                                
B   0  .                                                                                                                                
B   1  .                                                                                                                                
B   2  2                                                                                                                                
B   3  .                                                                                                                                
B   4  .                                                                                                                                
B   5  5                                                                                                                                
B   6  .                                                                                                                                
B   7  7                                                                                                                                
B   8  .                                                                                                                                
C   0  .                                                                                                                                
C   1  .                                                                                                                                
C   2  .                                                                                                                                
C   3  .                                                                                                                                
C   4  4                                                                                                                                
C   5  .                                                                                                                                
C   6  .                                                                                                                                
C   7  .                                                                                                                                
C   8  .                                                                                                                                
C   9  9                                                                                                                                
C  10  .                                                                                                                                
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  set have ;                                                                                                                            
  by wellbore ;                                                                                                                         
  inside_reservoir_flg + N (top_layer_depth) ;                                                                                          
  if last.wellbore then inside_reservoir_flg = 0 ;                                                                                      
run ;                          

 Or, amending @novinosrin's code to take advantage of auto-setting the non-retained flag variable to missing before the DoW-loop:

data want ;                                                                                                                             
  do until (last.wellbore) ;                                                                                                            
    set have ;                                                                                                                          
    by wellbore ;                                                                                                                       
    inside_reservoir_flg = sum (inside_reservoir_flg, N (top_layer_depth)) ;                                                            
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ; 

Kind regards

Paul D.

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
  • 8 replies
  • 793 views
  • 2 likes
  • 5 in conversation