SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Seek help on Create new var with Do Loop

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

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 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;

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

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.

View solution in original post


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

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: 28

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 244 views
  • 0 likes
  • 2 in conversation