I have a SAS dataset. that has a column that contains text data. There are instances where the field is empty.
But if we use the indicator >>if var=" " then x1=1;else x1=0;
Problem is, it is not capturing all the spaces. There are some spaces left, which still have value 0. I assume, these might be junk chars, which are not getting displayed. How can I deal with this situation?
I have already tried compress, strip,trim etc. I just printed the hex values and it seems it has 2 cases '00'(NULL) and '20'(space)
For space condition it works, but for null it fails. Is there a way to treat it, without coverting to HEX?
It's easy enough to change your code:
if var in (' ', '00'x) then x1=1;
else x1=0;
There are complications, however, if your variable is defined as being more than one character long. For example, if your variable is 5 characters long, you might actually have 5 nulls there, rather than one null with four blanks. The logic above wouldn't pick up 5 nulls.
For longer variables, another variation would be:
if var in : (' ', '00'x) then x1=1;
else x1=0;
This will compare just the first character of your variable, looking for blanks and nulls. That may be acceptable, but it depends on whether you might have data values that begin with a blank (or a null) but contain actual text afterwards.
The tools are there, it's really a matter of knowing what's in the data. Your best bet might be to forget about creating X1. Just use the TRANSLATE function to convert all nulls into blanks. That would certainly make later programming easier as well.
***** EDITED: I see that Tom already posted the syntax for TRANSLATE, as I was typing my answer.
Without seeing the data and the logic we wouldn't be able to say. The functions you have given: strip(), trim() remove blanks, compress() can be used to keep or drop various characters/groups of characters. They should be sufficient for the purpose.
You can use the COMPRESS function to remove the nulls ('00'X). Or you could change them to spaces by using the TRANSLATE() function.
var = translate(var,' ','00'x);
It's easy enough to change your code:
if var in (' ', '00'x) then x1=1;
else x1=0;
There are complications, however, if your variable is defined as being more than one character long. For example, if your variable is 5 characters long, you might actually have 5 nulls there, rather than one null with four blanks. The logic above wouldn't pick up 5 nulls.
For longer variables, another variation would be:
if var in : (' ', '00'x) then x1=1;
else x1=0;
This will compare just the first character of your variable, looking for blanks and nulls. That may be acceptable, but it depends on whether you might have data values that begin with a blank (or a null) but contain actual text afterwards.
The tools are there, it's really a matter of knowing what's in the data. Your best bet might be to forget about creating X1. Just use the TRANSLATE function to convert all nulls into blanks. That would certainly make later programming easier as well.
***** EDITED: I see that Tom already posted the syntax for TRANSLATE, as I was typing my answer.
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.