DATA Step, Macro, Functions and more

Set a value for all missing character variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Set a value for all missing character variables

Hi,

I am creating a set of tables and want to replace all character variables that are missing with a rudimentary value like 'UNKNOWN' for example.  I want to do this dynamically: e.g. not have to explicitly set the variable value to 'UNKNOWN'.  It should apply to all character variables.  I can't use a format to set it at runtime it needs to be written to the dataset.  I was hoping something like if(strip(_character_))='' then _character_ = 'UNKNOWN' works however this doesn't work.

Any ideas from anyone?  Any help is appreciated.


Accepted Solutions
Solution
‎05-11-2012 08:12 AM
Respected Advisor
Posts: 3,124

Re: Set a value for all missing character variables

Use 'do over' to save some typing:

data want ;

set class;

array _a $ _character_;

do over _a;

  if missing(_a) then _a='UNKNOWN';

end;

run;

Regards,

Haikuo

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Set a value for all missing character variables

data have;

length a b c d $8;

a='abc';

b='efg';

c=' ';

d=' ';

data want;

  set have;

  array _abc(*) _character_;

  do _n_=1 to dim(_abc);

    if _abc(_n_) =' ' then _abc(_n_)='UNKNOWN';

  end;

  proc print;run;

Super User
Posts: 9,671

Re: Set a value for all missing character variables

That would be a problem when your character variable is not long enough to hold UNKNOWN.

Thus, Assuming your character variable is long enough.

data class;
 set sashelp.class;
 if _n_ le ceil(ranuni(-1)*10) then call missing(name);
run;
data want ;
 set class;
 array _a{*} $ _character_;
 do i=1 to dim(_a);
  if missing(_a{i}) then _a{i}='UNKNOWN';
 end;
 drop i;
run;


Ksharp

Solution
‎05-11-2012 08:12 AM
Respected Advisor
Posts: 3,124

Re: Set a value for all missing character variables

Use 'do over' to save some typing:

data want ;

set class;

array _a $ _character_;

do over _a;

  if missing(_a) then _a='UNKNOWN';

end;

run;

Regards,

Haikuo

Occasional Contributor
Posts: 17

Re: Set a value for all missing character variables

Thanks for the responses everyone!

Valued Guide
Posts: 765

Re: Set a value for all missing character variables

hi ... another idea ...

data want;

set have;

array ch(*) _character_;

do _n_ = 1 to dim(ch);

    ch(_n_) = coalescec(ch(_n_), 'UNKNOWN');

end;

run;

ps ... http://www.sascommunity.org/wiki/Tips:Replace_Missing_Character_Values_with_a_Character_String

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 1399 views
  • 6 likes
  • 5 in conversation