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

Hello Everyone

 

Need one help

 

Old_Customer_id      Customer_id        Updated_ID (This is the column I need)

 

301                               100                     301

.                                    100                     301

                                     101                     101

305                               102                      305        

310                                102                     305

                                      103                    103

                                      104                    104

 

 

So I need the above results in the updated_id variable based on three conditions.

 

Condition 1 If customer_id has 101 value, then I need 301 in the updated_id variable in both rows . Zero should be ignored

Condition 2 If 102 customer_id has two values against it 305 and 310 in old_customer_id, then I need 305 in the updated_id as it is minimum in both rows

Condition 3: This is simple. If there are no values against that particular customer_id in the old_customer_id then I need customer_id value in the updated_id which is the case with 103 and 104.

 

I wrote a code using SQL lookup and removing zero,but it did not convince my client. I needs other approach because the data size is in millions.

 

 

If

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

If I understood your question.

 

data have;
input Old_Customer_id      Customer_id;
cards;
301                               100               
.                                    100                 
.                                     101                     
305                               102                      
310                                102                     
.                                      103                   
 .                                     104  
;
proc sort data=have out=temp;
by  Customer_id   ;
run;
data want;
 do until(last.Customer_id);
   set temp;
   by  Customer_id;
   want=min(want,Old_Customer_id)  ;
 end;
  do until(last.Customer_id);
   set temp;
   by  Customer_id;
   want=coalesce(want,Customer_id);
   output;
 end;
run;

View solution in original post

2 REPLIES 2
Ksharp
Super User

If I understood your question.

 

data have;
input Old_Customer_id      Customer_id;
cards;
301                               100               
.                                    100                 
.                                     101                     
305                               102                      
310                                102                     
.                                      103                   
 .                                     104  
;
proc sort data=have out=temp;
by  Customer_id   ;
run;
data want;
 do until(last.Customer_id);
   set temp;
   by  Customer_id;
   want=min(want,Old_Customer_id)  ;
 end;
  do until(last.Customer_id);
   set temp;
   by  Customer_id;
   want=coalesce(want,Customer_id);
   output;
 end;
run;
sameer112217
Quartz | Level 8

thanks ksharp... thats wonderful ..it works

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 719 views
  • 1 like
  • 2 in conversation