BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
animesh123
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
animesh123
Obsidian | Level 7
There is nothing with missing, But if my requirement are to Replace . with 9 , What does I required to change in my present below code
Data output;
set input;
Array num _numeric_;
do over nums;
if Num=. then nums=9
end;

Input :
Name Rank RollNO Plan
Animesh 1 2 55
Mardi . . 65

Output:
Name Rank RollNO Plan
Animesh 1 2 55
Mardi 9 9 65
Tom
Super User Tom
Super User

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?

animesh123
Obsidian | Level 7
Its just a question, if I wanna replace . with 9 only on Rank and Rollno not on Plan
I forgot to define
Name(Char) Rank(Num) RollNO(Num) Plan(Num)
Data output;
set input;
Array num _numeric_;
do over nums;
if Num=. then nums=9
end;

Input :
Name Rank RollNO Plan
Animesh 1 2 55
Mardi . . 65

Output:
Name Rank RollNO Plan
Animesh 1 2 55
Mardi 9 9 65
Tom
Super User Tom
Super User

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.

https://xyproblem.info/ 

animesh123
Obsidian | Level 7
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
Tom
Super User Tom
Super User

@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
Obsidian | Level 7
So to provide you the answer for why. I need to explain you the explanation whole process of where and what is .
Hey man I am just new guy working in SAS tool had a little bit of experience not so much just fumbled on one of the code where I need to replace . to 9 .its simple as that
to tell you the truth some of your why question i didn't even get
But yeah Thank you very much for you Answer
PaigeMiller
Diamond | Level 26

@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

--
Paige Miller
Reeza
Super User

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;
animesh123
Obsidian | Level 7
Thanks Reeza, I have used it like this
Data output;
Set input;
Array Nums Rank Rollno;
do over Nums;
if Num=. then Nums=9
end;

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
  • 12 replies
  • 1593 views
  • 0 likes
  • 4 in conversation