An efficient way to Copying values in subsequent records - SAS

前端 未结 3 1496
陌清茗
陌清茗 2020-12-21 11:36

I have a dataset that is grouped by category variables in the source data. For example:

Bar  | Foo1
     | Foo2
     | Foo3
Bar2 | Foo4
     | Foo5
     | Fo         


        
相关标签:
3条回答
  • 2020-12-21 11:51

    You can retain the non-missing value and apply it to any missing values, e.g.

    data want ;
      set have ;
    
      length last $10. ;
      retain last '' ;
    
      if not missing(PARENT) then last = PARENT ;
      else PARENT = last ;
    
      drop last ;
    run ;
    
    0 讨论(0)
  • 2020-12-21 11:53

    Another way of doing it is to add a dummy variable, then trick SAS by using Update statement.

    data have;
    infile cards dlm='|';
        input (var1 var2) (:$8.);
        retain idx 1;
        cards;
    Bar  | Foo1
         | Foo2     
         | Foo3
    Bar2 | Foo4
         | Foo5
         | Foo6
         ;
    
    data want;
        update have (obs=0) have;
        by idx;
        output;
        drop idx;
    run;
    
    0 讨论(0)
  • 2020-12-21 12:07

    The lag function returns the last value that was passed to it when it was called, not the value that was last output.

    You could do something like the following:

    data want;
        set have;
        length last_parent $256;
        retain last_parent;
    
        if parent = "" then parent = last_parent;
        else last_parent = parent;
    
        drop last_parent;
    run;
    

    You would need to set the length of last_parent to the same length as parent to make sure nothing gets cut off.

    0 讨论(0)
提交回复
热议问题