BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ifb10
Obsidian | Level 7

Hello everyone,

 

I have a large excel sheet with around 100.000 data points of following structure:

IDValue
13
13
13
22
23
24
20
20
39
310
310
30
30
30
30

 

I would like to extend the last non 0 value for each ID, to replace the 0s. The final table should look like this:

IDValue
13
13
13
22
23
24
24
24
39
310
310
310
310
310
310

 

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller
ifb10
Obsidian | Level 7

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!

PeterClemmensen
Tourmaline | Level 20
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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 311 views
  • 0 likes
  • 3 in conversation