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-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    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.

    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
    • 6 replies
    • 1086 views
    • 6 likes
    • 4 in conversation