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.
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
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;
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
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
Thanks for the responses everyone!
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.