sas_kappel
sas_kappel

Reputation: 61

Alternative coding than lag function?

I would like to have a more efficient way perform this imputation. What I want is the value of variable CID to be copied on the row where CID is missing. For instace having CID=1818 reported also for dates 1,14,28 and 42.

The program that I wrote works fine but I would like to know if there is another more simple way to perform this action. Note that here RETAIN can't be used.

DATA test;
    infile cards dlm=''  dsd ;
    input cid $  @6 days  $ @9 CH  @13  CL ;

    cards;
    1818 -2 117 46
         1  107 45
         14 97  46
         28 104 46
         42 106 44
    5684 -2 100 62
         1  58  78
         14 87  46
         28 102 45
         42 155 41
         ;
    RUN;


    options mprint mlogic symbolgen;
    %macro lag(var,num);
    %do i=2 %to &num.;
    sub&i.=lag&i.(&var);
    if cid=' ' then cid=sub&i.;
    /*drop sub&i.;*/
    %end;
    %mend lag;

    data test_1 ;set test;
    sub=lag(cid);
    if cid=' ' then cid=sub;
    %lag(cid,5);
    run;

Upvotes: 0

Views: 453

Answers (1)

Joe
Joe

Reputation: 63424

Why can't Retain be used?

data want;
  set test;
  retain _cid;
  if not missing(cid) then _cid=cid;
  else cid=_cid;
  drop _cid;
run;

Upvotes: 3

Related Questions