- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the responses everyone!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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