Hello everyone,
I have a large excel sheet with around 100.000 data points of following structure:
| ID | Value |
| 1 | 3 |
| 1 | 3 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
| 2 | 0 |
| 2 | 0 |
| 3 | 9 |
| 3 | 10 |
| 3 | 10 |
| 3 | 0 |
| 3 | 0 |
| 3 | 0 |
| 3 | 0 |
I would like to extend the last non 0 value for each ID, to replace the 0s. The final table should look like this:
| ID | Value |
| 1 | 3 |
| 1 | 3 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
| 2 | 4 |
| 2 | 4 |
| 3 | 9 |
| 3 | 10 |
| 3 | 10 |
| 3 | 10 |
| 3 | 10 |
| 3 | 10 |
| 3 | 10 |
Does anybody have a suggestions? I have tried with lag function with loops but can not seem to find the right solution.
Thank you for any advice!
data want;
set have;
by id;
retain newvalue;
prev_value=lag(value);
if first.id or value>0 then newvalue=value;
if value=0 and not first.id and prev_value^=0 then newvalue=prev_value;
drop prev_value;
run;
Please do us a favor, help us out (which also helps you get quicker and better answers), from now on do not provide example data as screen capture or as file attachments; instead provide data as working SAS data step code.
data want;
set have;
by id;
retain newvalue;
prev_value=lag(value);
if first.id or value>0 then newvalue=value;
if value=0 and not first.id and prev_value^=0 then newvalue=prev_value;
drop prev_value;
run;
Please do us a favor, help us out (which also helps you get quicker and better answers), from now on do not provide example data as screen capture or as file attachments; instead provide data as working SAS data step code.
Thank you this does the trick. I have been trying to overwrite the original value instead of creating a new one. This is a very simple and effective solution!
data have;
input ID Value;
datalines;
1 3
1 3
1 3
2 2
2 3
2 4
2 0
2 0
3 9
3 10
3 10
3 0
3 0
3 0
3 0
;
data want;
set have;
by ID;
if Value then _iorc_ = Value;
if not first.id and Value = 0 then Value = _iorc_;
run;
Result:
ID Value 1 3 1 3 1 3 2 2 2 3 2 4 2 4 2 4 3 9 3 10 3 10 3 10 3 10 3 10 3 10
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.