Solved
Contributor
Posts: 34

# Seek help on Create new var with Do Loop

[ Edited ]
``````I want to create new variables (newna1-newna5) from na1 -na5, if there is missing value of na then newna will be equal to the previous nonmissing value of na.But I have problems running the program below, error with array length.Please help me to figure it out.Thank you very muchdata try;
input id na1 na2 na3 na4 na5;
datalines;
1 2 3 . . 4
2 3 5 4 1 .
3 6 . 7 5 4
4 5 3 . 4 .
5 4 . . . 6
;
run;

data try1;
set try;
array na(*) na1-na5;
array newna(*) newna1-newna5;

do i=1 to 5;
newna(i)=na(i);
do j=1 to 4;
if na(i)=. then newna(i)=na(i-j);
end;

end;
run;

output;``````

Accepted Solutions
Solution
‎04-19-2016 06:40 PM
Super User
Posts: 6,641

## Re: Seek help on Create new var with Do Loop

You could simplify the process and get the correct result most of the time:

``````data try1;
set try;
array na(*) na1-na5;
array newna(*) newna1-newna5;

do i=1 to 5;
if na(i) > . then newna(i)=na(i);
else newna(i)=newna(i-1);
end;
run;``````

When I say "most of the time" I'm looking at the possibility that na1 might be missing.  What should happen in that case?  The program will generate an error as it stands now.  So if it's possible that na1 might be missing, you would need to specify what the result should be so the program can be modified.

All Replies
Solution
‎04-19-2016 06:40 PM
Super User
Posts: 6,641

## Re: Seek help on Create new var with Do Loop

You could simplify the process and get the correct result most of the time:

``````data try1;
set try;
array na(*) na1-na5;
array newna(*) newna1-newna5;

do i=1 to 5;
if na(i) > . then newna(i)=na(i);
else newna(i)=newna(i-1);
end;
run;``````

When I say "most of the time" I'm looking at the possibility that na1 might be missing.  What should happen in that case?  The program will generate an error as it stands now.  So if it's possible that na1 might be missing, you would need to specify what the result should be so the program can be modified.

Contributor
Posts: 34

## Re: Seek help on Create new var with Do Loop

Dear Astouding,

Thanks a lot for your help.

It's so simple!

Best,

Trang

🔒 This topic is solved and locked.