Another question about missing value

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 130
Accepted Solution

Another question about missing value

I have a dataset with 30 variables, each variables have its own unique name like sector code, earning ratio, country code, etc., so all variable names don't follow any pattern such as se_1 se_2...se_30. 

I am trying to write a statement something like:

data want;

    set have;

    if TYPE='PARMS' and any of (variable2 to variable30) =.  then set missing value(s)=0;

run;

or, how to add the condition TYPE='PARMS' to the following code? so that the following code will not replace all missing values in the entire dataset eqt 0?

data want;

   set have;

   array change _numeric_;

      do over change;

          if change=. then change=0;

      end;

run;


Accepted Solutions
Solution
‎08-18-2014 09:30 AM
Super User
Super User
Posts: 6,318

Re: Another question about missing value

Perhaps I am misunderstanding the question, but it sounds very simple.

I interpret the question to be:

  When variable TYPE has the value 'PARMS' then I want to convert all numeric missing values to zero.

So you just need to add the test for TYPE='PARMS' to your existing code.

data want;

  set have;

  array change _numeric_;

  if type='PARMS' then do over change;

    if change=. then change=0;

  end;

run;

View solution in original post


All Replies
Grand Advisor
Posts: 17,325

Re: Another question about missing value

If the variables are beside each other you can use the -- short cut.

For example if age, weight, height are the variables in order you can refer to them as age--height

I'm not sure what you mean if you don't want to set the missing to 0, what do you want to do, flag them?

Frequent Contributor
Posts: 130

Re: Another question about missing value

thank you for your reply. I mean I want to set missing=0 with the condition that TYPE='PARMS' (I don't wanna set all missing in the entire dataset =0)

Grand Advisor
Posts: 9,571

Re: Another question about missing value

How about :

data class;
 set sashelp.class;
 if _n_ lt 9 then call missing(age,height,weight);
run;

proc stdize data=class(where=(sex='F')) out=Female missing=0 reponly;
run;
data want;
 set class(where=(sex ne 'F')) Female;
 run;

Xia Keshan

Frequent Contributor
Posts: 130

Re: Another question about missing value

Thank you Keshan! the code also works great.

Solution
‎08-18-2014 09:30 AM
Super User
Super User
Posts: 6,318

Re: Another question about missing value

Perhaps I am misunderstanding the question, but it sounds very simple.

I interpret the question to be:

  When variable TYPE has the value 'PARMS' then I want to convert all numeric missing values to zero.

So you just need to add the test for TYPE='PARMS' to your existing code.

data want;

  set have;

  array change _numeric_;

  if type='PARMS' then do over change;

    if change=. then change=0;

  end;

run;

Frequent Contributor
Posts: 130

Re: Another question about missing value

Yes, that's what I want. Thanks!  Before you guys helping out, I tried the following, both didn't work. Now I see the problem.

data want;

  set have;

    where type='PARMS' ; (1)

    array change _numeric_;

    if change=. then change=0;

  end;

run;

and:

data want;

  set have;

     array change _numeric_;

    if type='PARMS' ; (2)

    if change=. then change=0;

  end;

run;

Respected Advisor
Posts: 4,962

Re: Another question about missing value

Quite reasonably, you will have to tell SAS which variables you want to process.  There is no software that knows how to process only the subset of variables that you are thinking of.  As Reeza indicated, however, there are certain ways to abbreviate a list of variables within SAS.  The one that you refer to, such as se_1 - se_10, is only one type of variable list.  Similarly, _numeric_ is only one such list.  Here are some others:

cat--dog  = all variables in the order in which they were created, beginning with CAT and ending with DOG

cat-numeric-dog  =  all NUMERIC variables from the cat--dog list

You need to know something about the order in which your variables were created.  PROC CONTENTS will tell you that.

Finally, note that changing missing values to 0 is only occasionally a good idea.  But that's another subject.

Good luck.

Frequent Contributor
Posts: 130

Re: Another question about missing value

Thank you for your inputs Astounding!

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 320 views
  • 6 likes
  • 5 in conversation