BookmarkSubscribeRSS Feed
geneshackman
Pyrite | Level 9

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

 

4 REPLIES 4
yabwon
Onyx | Level 15

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

 

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



geneshackman
Pyrite | Level 9
Hi Bart. Good point. I checked. The variable is character, so I'll try some of your solutions. Thanks.
Quentin
Super User

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 ;
The Boston Area SAS Users Group is hosting free webinars!
Next up: Lisa Mendez & Richann Watson present Get Tipsy with Debugging Tips for SAS® Code: The After Party on Wednesday Jul 16.
Register now at https://www.basug.org/events.
geneshackman
Pyrite | Level 9

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 4 replies
  • 1013 views
  • 2 likes
  • 3 in conversation