BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JessSun
Calcite | Level 5

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:

    1 ACCEPTED SOLUTION

    Accepted Solutions
    Reeza
    Super User

    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

    6 REPLIES 6
    Reeza
    Super User

    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;

    JessSun
    Calcite | Level 5

    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

    Reeza
    Super User

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

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

    robertrao
    Quartz | Level 8

    Hi,

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

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

    Thanks

    Reeza
    Super User

    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. 

    art297
    Opal | Level 21

    Change your loop to start at 2 rather than 1.

    SAS Innovate 2025: Save the Date

     SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

    Save the date!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

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