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

Hello, I would like to replace value in the amount with last observation amount value when only the description is "r" or "ra" within same account. Could you please help me how to do that ? Thank you so much !

data have;

infile datalines delimiter=',';

input account description $ amount;

datalines;

123,a,0

123,a,0

123,r,1    /**<- replace with last value (30) when "r" or "ra" */

123,r,20   /**<- replace with last value (30) when "r" or "ra" */

123,ra,30

123,w,4

234,b,1

234,c,20

234,ra,400  /**<- replace with last value (200) when "r" or "ra" */

234,r,200

234,c,200

;

run;

data want;

infile datalines delimiter=',';

input account description $ amount;

datalines;

123,a,0

123,a,0

123,r,30

123,r,30

123,ra,30

123,w,4

234,b,1

234,c,20

234,ra,200

234,r,200

234,c,200

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
pradeepalankar
Obsidian | Level 7

data have;

infile datalines delimiter=',';

input account description $ amount;

datalines;

123,a,0

123,a,0

123,r,1

123,r,20

123,ra,30

123,w,4

234,b,1

234,c,20

234,ra,400

234,r,200

234,c,200

;

run;

data test;

set have(where =(description in ('r','ra')));

by account;if last.account then output ;run;

proc sql;

update have 

set amount=(select amount from test where test.account=have.account )

where have.description in ('r','ra');

quit;

View solution in original post

4 REPLIES 4
TarunKumar
Pyrite | Level 9

there is huge diff in input data and ouput data. pls check and pos your requirement again.

for eg 123,r,30 and 234,r,300 is not avilable in have data set.

davidnamh
Calcite | Level 5

HI TarunKumar,

I have updated my codes for your review. Thanks !

pradeepalankar
Obsidian | Level 7

data have;

infile datalines delimiter=',';

input account description $ amount;

datalines;

123,a,0

123,a,0

123,r,1

123,r,20

123,ra,30

123,w,4

234,b,1

234,c,20

234,ra,400

234,r,200

234,c,200

;

run;

data test;

set have(where =(description in ('r','ra')));

by account;if last.account then output ;run;

proc sql;

update have 

set amount=(select amount from test where test.account=have.account )

where have.description in ('r','ra');

quit;

davidnamh
Calcite | Level 5

Thank you so much pradeepalankar !

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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