BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
munitech4u
Quartz | Level 8

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tom
Super User Tom
Super User

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);
Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1644 views
  • 1 like
  • 4 in conversation