Hi all. I hope everyone is healthy.
Hopefully a very simple question. How to write * when the data are blank or have "."?
I have this code
if tbr2="." then tbr2="*";
if tpr2="." then tpr2="*";
I -think- the data set has "." as values that mean blank or missing, but that could be the problem too.
So, first question: would that code work, if the values are "."?
Thanks
1) If you variable is numeric you won't be able to replace it with "*", in this case you could only write a format that displays missing value as asterisk, something like:
proc format;
value missinAsterisk
low-high = [best32.]
other = "*"
;
run;
data _null_;
do i = -1,100,3.14,.,.A,.Z,._,-17,42;
put i missinAsterisk.;
end;
run;
2) If you have character variable then the condition:
var = " "
checks if the variable is blank (missing). In this case you can do it:
if tbr2=" " then tbr2="*";
3) If your variable is character and contains "."(dot) then it is not missing 🙂 but... if you want to replace all dots in your variable you could try to use translate() function:
data _null_;
do x = "...", ".", " .", ". .";
y = translate(x,"*",".");
put x= / y=;
end;
run;
Bart
If it's character, and '.' means missing, I think it's better to convert to a character misssing (blank) rather than to an asterisk. If it's a character missing, then SAS knows that is missing. If it's a *, then you as the programmer need to remember that * means missing and code around it. I like the format approach that Bart showed. It allows you to keep the value as missing, but display it as an asterisk, e.g.:
data have ;
do x = "A","B"," ",".","D";
output ;
end;
run ;
proc format;
value $ missinAsterisk
' '='*'
other = [$char8.]
;
run;
data want ;
set have ;
array chars {*} _character_ ;
do i=1 to dim(chars) ;
if chars{i}='.' then call missing(chars{i}) ;
end ;
drop i ;
format _character_ $missinAsterisk. ;
run ;
proc print data=want ;
run ;
Thanks Bart and Quentin. I examined the data set once more, because of your responses, and it turns out the problem was that the value wasn't "." but " .". It had spaces in front of the period. I put in the spaces, and that worked.
if tbr2=" ." then tbr2="*";
if tpr2=" ." then tpr2="*";
Thanks again, and I apologize for not examining the data more closely in the first place. Who would have thought!
Gene
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.