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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 9447 views
  • 8 likes
  • 5 in conversation