I am trying to copy one cell to the next cell when another column has the same value. I have been using this code, but it doesn't seem to be working the way i want.
if TRIP_ID=Lag(TRIP_ID) then CHRG_CD=lag(CHRG_CD);
The trip_ID is 349333
10791391 | 349333 | |
10791390 | 349333 | 3892B |
10791390 | 349333 | 3892B |
So where does CHRG_CD come into the program?
Are you trying to fill in the blank cell with the later value from Bundle?
That's a common mistake. The LAG function does not retrieve the value from the previous observation. It actually does something a little bit different. It retrieves the value from the last time that the LAG function executed.
Fixing this will be relatively simple. But you need to provide a little more information. In the data you provided, what are the column names? What end result would you like to see?
So where does CHRG_CD come into the program?
Are you trying to fill in the blank cell with the later value from Bundle?
OK. Most solutions will assume that the data is in order such as BY TRIP_ID. If that's not possible, more difficult approaches are possible.
There also may be different BUNDLE values within the same TRIP_ID that will get replaced with a common value.
At any rate here's one possibility that assumes a sorted order:
data want;
do until (last.TRIP_ID);
set have;
by trip_ID;
if bundle > ' ' then replacement_bundle=bundle;
end;
do until (last.TRIP_ID);
set have;
by trip_ID;
output;
end;
drop bundle;
rename replacement_bundle=bundle;
run;
The top loop finds a nonmissing BUNDLE for that TRIP_ID. The bottonm loop reads the same observations and uses that nonmissing value as a replacement value.
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!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.