SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CameronL
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

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;

Ksharp
Super User

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

Haikuo
Onyx | Level 15

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

CameronL
Fluorite | Level 6

Thanks for the responses everyone!

MikeZdeb
Rhodochrosite | Level 12

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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 5 replies
  • 10721 views
  • 8 likes
  • 5 in conversation