BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GN0001
Barite | Level 11

Hello all,

I wrote a loop that goes through variables to replace dot (a missing value) with zero; Now I want to add another loop to go through observations as well. 

This is my code and your help is greatly appreciated. Blue Blue

Data simpleData;
Input Var1 Var2 Var4 Var5 Var6 Var7 Var8 ;

cards;

5 7 3 6 5 8 3

4 7 3 6 5 8 3

3 6 3 3 5 8 3

5 4 3 . 3 8 3

5 4 3 3 3 8 3

5 . 3 3 3 8 3

5 7 3 3 3 8 1

5 7 3 3 3 8 0

run;
data mathtop;
set simpleData;
varResult = var1 * var2;
Array varArr(7) Var1 Var2 Var4 Var5 Var6 Var7 Var8;
Do i = 1 to 7;
if varArr(i)=. Then varArr(i)=0;
var9 = var4 + var8;
end;
Run;

 

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

You don't need a loop to process observations as SAS automatically will process all rows one after the other.

 

View your MATHTOP dataset to prove this program is already working correctly.

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star

You don't need a loop to process observations as SAS automatically will process all rows one after the other.

 

View your MATHTOP dataset to prove this program is already working correctly.

Patrick
Opal | Level 21

Besides of a do loop you could also use Proc Stdize as proposed in the solution here

PaigeMiller
Diamond | Level 26

I hope you have a really really really good reason why you want to replace missings with zeros, as there are plenty of situations where this is not a good idea (and a few situations where this makes sense). A word to the wise...

--
Paige Miller
Astounding
PROC Star
One example of a program that doesn't need to replace missing values with zero is the one you posted. You can get the same result by replacing everything from the array statement to the end with:

var9 = sum(0, var4, var8);

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1406 views
  • 4 likes
  • 5 in conversation