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 much
data 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;
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.
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.
Dear Astouding,
Thanks a lot for your help.
It's so simple!
Best,
Trang
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.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.