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
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:
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
do i=2 to dim(weight);
if weight=. then weight=weight[i-1];
end;
drop i;
run;
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
do i=2 to dim(weight);
if weight=. then weight=weight[i-1];
end;
drop i;
run;
Thanks Reeza and Arthur!
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
What's the output you get vs the output you want?
If it looks back and its missing it will stay missing.
Hi,
I am curious to know why the records dont show up when use :
do i= 1 to dim(weight)???
Thanks
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.
Change your loop to start at 2 rather than 1.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.