The below code will change the output data from . to 9
Data output;
set input;
Array num _numeric_;
do over nums;
if Num=. then nums=9
end;
Let say if we wanna change . to 9 on a particular variable like only on(
Rank RollNO )
, As if i have a table and the variable are
Name Rank RollNO Plan
Animesh 1 2 55
Mardi . . 65
This is not correct code, NUM/NUMS are being used interchangeabley and I don't recommend using implicit arrays and do over without indexes if you don't understand arrays well.
@animesh123 wrote:
The below code will change the output data from . to 9
Data output;
set input;
Array num _numeric_;
do over nums;
if Num=. then nums=9
end;
Let say if we wanna change . to 9 on a particular variable like only on(
Rank RollNO )
, As if i have a table and the variable are
Name Rank RollNO Plan
Animesh 1 2 55
Mardi . . 65
For a single variable:
data want;
set have;
if rank = . then rank=9;
run;
For multiple variables use an array but explicitly list the variables in the array:
data want;
set have;
array _nums(*) rank rollNo; *list variables to convert here;
do i=1 to dim(_nums);
if _nums(i) = . then _nums(i) = 9;
end;
run;
This isn't hard to do, but missings shouldn't usually be replaced by a numeric value. If you ever want to then find a mean or a sum, the value of 9 causes the mean or sum to be incorrect. So I advise against doing this. What's wrong with leaving this as a missing?
Why are you looping over an array named NUMS that you never defined?
Why are you changing the value of NUMS based on whether or not the value in NUM is a missing value?
And WHY do you want to convert missing values into valid numbers? That will make your variables difficult (if not impossible) to work with.
And you definitely do NOT want to change the missing values to 9 in a variable like PLAN that clearly seems to allow values that are larger than 9. Why would you want to convert missing PLAN to PLAN number 9?
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
Then don't include PLAN in the list of variables you put into the array.
array nums rank rollno ;
do over nums;
nums=coalesce(nums,9);
end;
If you have a LOT of variables and are too lazy to figure out how specify them all without including plan then exclude it inside the loop.
array nums _numeric_ ;
do over nums;
if lowcase(vname(nums)) ne 'plan' then
nums=coalesce(nums,9)
;
end;
Or define the array BEFORE the data step complier knows about PLAN so it is not included by the variable list _NUMERIC_.
data want;
set have(drop=plan);
array nums _numeric_ ;
set have;
do over nums;
nums=coalesce(nums,9);
end;
run;
But you still did not explain WHY you want to make your variables unusable.
This looks like an XY problem.
@animesh123 wrote:
Hey Tom
Thanks. For me its usable data .
To answer your question All the data will get drop in further process
wherever it contains 9 .
I am making it 9 so the other data dont get messed up
before the code was changing all of the variable to 9 which i dont required
Huh?
Why not just remove whatever logic was being used that dropped the values that had a 9?
Or did you mean the missing values were dropped?
If so then what analysis are you doing that is dropping the missing observations? Any analysis of say the MEAN of the variable SHOULD exclude the missing values. Replacing missing with 9 and then trying to calculate the MEAN will result in a meaningless value.
@animesh123 wrote:
I am making it 9 so the other data dont get messed up
Many SAS PROCs have a MISSING option, "so the other data don't get messed up". But enough beating around the bush. Please explain what you are doing where other data can get messed up. We need to know, because I think your attempts to turn a missing into a 9 are very misguided. But maybe if we knew what you are doing, we could suggest better methods, or maybe even we could see that its okay to turn a missing into a 9 (but I consider this unlikely).
Tell us what you are doing that requires you to turn a missing into a 9
This is not correct code, NUM/NUMS are being used interchangeabley and I don't recommend using implicit arrays and do over without indexes if you don't understand arrays well.
@animesh123 wrote:
The below code will change the output data from . to 9
Data output;
set input;
Array num _numeric_;
do over nums;
if Num=. then nums=9
end;
Let say if we wanna change . to 9 on a particular variable like only on(
Rank RollNO )
, As if i have a table and the variable are
Name Rank RollNO Plan
Animesh 1 2 55
Mardi . . 65
For a single variable:
data want;
set have;
if rank = . then rank=9;
run;
For multiple variables use an array but explicitly list the variables in the array:
data want;
set have;
array _nums(*) rank rollNo; *list variables to convert here;
do i=1 to dim(_nums);
if _nums(i) = . then _nums(i) = 9;
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.