Help using Base SAS procedures

Impute missing date using LOCF

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Impute missing date using LOCF

Hi everyone,

I have a sample dataset as follows:

id weight1 weight2 weight3 weight4

1    50.1       51.8         .          53.6

2    70.8           .           .              .

3        .         49.8     52.9        .

4        .         68.1         .          66.9

5    48.0       49.1         .              .

I have tried the command as below,

data locf;

set tweight;

array weight

  • weight:;
  • do i=1 to dim(weight);

    if weight=. then weight=weight[i-1];

    end;

    drop i;

    run;

    Then the output is as follows:

    id weight1 weight2 weight3 weight4

    1    50.1       51.8      51.8       53.6

    2    70.8      70.8      70.8       70.8

    BUT that is not what I expected because there are all missing after id 2. :smileyconfused::smileyconfused:

    I am trying to amend the command......and produce the output as below,

    id weight1 weight2 weight3 weight4

    1    50.1       51.8      51.8       53.6

    2    70.8      70.8      70.8       70.8

    3       .         49.8      52.9       52.9

    4              68.1      68.1       66.9

    5    48.0       49.1      49.1       49.1

    I think I missed ONE "if XXXXXX" or "else XXXXXX"~Please help~:smileyplus:


    Accepted Solutions
    Solution
    ‎06-12-2013 11:31 AM
    Super User
    Posts: 17,963

    Re: Impute missing date using LOCF

    I think you need to add a condition to not check the first one, probably the easiest way, start your loop from two probably?

    data locf;

    set tweight;

    array weight

  • weight:;
  • do i=2 to dim(weight);

    if weight=. then weight=weight[i-1];

    end;

    drop i;

    run;

    View solution in original post


    All Replies
    Solution
    ‎06-12-2013 11:31 AM
    Super User
    Posts: 17,963

    Re: Impute missing date using LOCF

    I think you need to add a condition to not check the first one, probably the easiest way, start your loop from two probably?

    data locf;

    set tweight;

    array weight

  • weight:;
  • do i=2 to dim(weight);

    if weight=. then weight=weight[i-1];

    end;

    drop i;

    run;

    Occasional Contributor
    Posts: 8

    Re: Impute missing date using LOCF

    Thanks Reeza and Arthur!Smiley Happy

    What if one of the id (id 10) has only one observation as follows;

    id weight1 weight2 weight3 weight4

    9        .       41.7        41.7       40.8<-------i.e. Only keep the first one as missing value

    10      .           .             .          49.0<-------i.e. Trying to keep the missing for the first three~:smileyconfused:

    11  48.1     48.1        48.1       46.5

    Super User
    Posts: 17,963

    Re: Impute missing date using LOCF

    What's the output you get vs the output you want?

    If it looks back and its missing it will stay missing.

    Super Contributor
    Posts: 1,040

    Re: Impute missing date using LOCF

    Hi,

    I am curious to know why the records dont show up when use :

    do i= 1 to dim(weight)???

    Thanks

    Super User
    Posts: 17,963

    Re: Impute missing date using LOCF

    Let's say i=1.

    if weight[1]=. then weight[1]=weight[1-1];


    which is:


    if weight[1]=. then weight[1]=weight[0];


    You don't have a weight[0] so SAS is writing errors to your log, I'd bet.

    Check your log when you submit that code. 

    PROC Star
    Posts: 7,366

    Re: Impute missing date using LOCF

    Change your loop to start at 2 rather than 1.

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 6 replies
    • 274 views
    • 6 likes
    • 4 in conversation