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

Hello,

 

Is there a way i could replace a row value to its previous row by each group. Below is the before and after dataset. Product for each type - C needs to be changed as type- L for each customer when the ID is same it has the highest amount.

 

Before:

CustomerIDTypeProductAmount
112432LA                         234
112432CB                            -  
223213LC                    23,212
223145LD                         335
223145CE                            -  
321311LF                         323
321324LG                      2,344
321324LH                           34
321324CI                            -  

 

After:

CustomerLINK_IDTypeProductAmount
112432LA               234
112432CA                  -  
223213LC          23,212
223145LD               335
223145CD                  -  
321311LF               323
321324LG            2,344
321324LH                 34
321324CG                  -  

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input Customer LINK_ID Type $ Product $ Amount;
datalines;
1 12432 L A 23
1 12432 C B 0
2 23213 L C 234
2 23145 L D 25
2 23145 C E 0
3 21311 L F 34
3 21324 L G 45
3 21324 L H 35
3 21324 C I 0
;
proc sql;
create table temp as
select * from have 
 where Type='L'
  group by Customer, LINK_ID
   having amount=max(amount);
quit;
data want;
 if _n_=1 then do;
  if 0 then set temp;
  declare hash h(dataset:'temp');
  h.definekey('Customer', 'LINK_ID');
  h.definedata('Product');
  h.definedone();
 end;
set have;
if Type='C' then rc=h.find();
drop rc;
run;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Use a retained variable:

data want;
set have;
retain _product;
if amount ne . then _product = product;
if amount = . and lag(customer) = customer then product = _product;
run;

Untested; for tested code, supply data in usable form (data step with datalines).

SD714
Fluorite | Level 6

Thank you for your reply. It is still giving me the same product for type- C.

 

Obs Customer LINK_ID Type Product Amount _product123456789

112432LA23A
112432CB0B
223213LC234C
223145LD25D
223145CE0E
321311LF34F
321324LG45G
321324LH35H
321324CI0I
SD714
Fluorite | Level 6
Here is the dataset.

data ccmrdata.chgdata;
input Customer LINK_ID Type $ Product $ Amount;

datalines;

1 12432 L A 23

1 12432 C B 0

2 23213 L C 234

2 23145 L D 25

2 23145 C E 0

3 21311 L F 34

3 21324 L G 45

3 21324 L H 35

3 21324 C I 0


;

run;

proc print data=ccmrdata.chgdata;

Obs Customer LINK_ID Type Product Amount
1 1 12432 L A 23
2 1 12432 C B 0
3 2 23213 L C 234
4 2 23145 L D 25
5 2 23145 C E 0
6 3 21311 L F 34
7 3 21324 L G 45
8 3 21324 L H 35
9 3 21324 C I 0
Kurt_Bremser
Super User

Then try this:

data have;
input Customer LINK_ID Type $ Product $ Amount;
datalines;
1 12432 L A 23
1 12432 C B 0
2 23213 L C 234
2 23145 L D 25
2 23145 C E 0
3 21311 L F 34
3 21324 L G 45
3 21324 L H 35
3 21324 C I 0
;

data want;
set have;
by customer;
retain _product _amount;
if first.customer
then do;
  _product = " ";
  _amount = .;
end;
if amount > _amount and type = 'L'
then do;
  _amount = amount;
  _product = product;
end;
if type = 'C' and _product ne ' ' then product = _product;
drop _:;
run;
mkeintz
PROC Star

@SD714 wrote:

Hello,

 

Is there a way i could replace a row value to its previous row by each group. Below is the before and after dataset. Product for each type - C needs to be changed as type- L for each customer when the ID is same it has the highest amount.

 

Before:

Customer ID Type Product Amount
1 12432 L A                          234
1 12432 C B                             -  
2 23213 L C                     23,212
2 23145 L D                          335
2 23145 C E                             -  
3 21311 L F                          323
3 21324 L G                       2,344
3 21324 L H                            34
3 21324 C I                             -  

 

After:

Customer LINK_ID Type Product Amount
1 12432 L A                234
1 12432 C A                   -  
2 23213 L C           23,212
2 23145 L D                335
2 23145 C D                   -  
3 21311 L F                323
3 21324 L G             2,344
3 21324 L H                  34
3 21324 C G                   -  

 

Thank you!


I thought your rule was supposed to find, for each customer, the product code that is found in the type L record with maximum amount and assign it as product code for any records with type="C".  That's what you apparently do for customer 3  (the last record get a "G" from two records prior).  But for customer 2, you want the type='C' record to get product='D', which is NOT associated with the maximum type-L amount.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input Customer LINK_ID Type $ Product $ Amount;
datalines;
1 12432 L A 23
1 12432 C B 0
2 23213 L C 234
2 23145 L D 25
2 23145 C E 0
3 21311 L F 34
3 21324 L G 45
3 21324 L H 35
3 21324 C I 0
;
proc sql;
create table temp as
select * from have 
 where Type='L'
  group by Customer, LINK_ID
   having amount=max(amount);
quit;
data want;
 if _n_=1 then do;
  if 0 then set temp;
  declare hash h(dataset:'temp');
  h.definekey('Customer', 'LINK_ID');
  h.definedata('Product');
  h.definedone();
 end;
set have;
if Type='C' then rc=h.find();
drop rc;
run;
SD714
Fluorite | Level 6
This worked as expected. Thank you so much!

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!

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
  • 8 replies
  • 1859 views
  • 1 like
  • 4 in conversation