DATA Step, Macro, Functions and more

Working with junk chars in SAS, while creating new variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

Working with junk chars in SAS, while creating new variables

[ Edited ]

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?


Accepted Solutions
Solution
‎08-04-2016 03:46 AM
Super User
Posts: 5,505

Re: Working with junk chars in SAS, while creating new variables

[ Edited ]
Posted in reply to munitech4u

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


All Replies
Super User
Super User
Posts: 7,955

Re: Working with junk chars in SAS, while creating new variables

Posted in reply to munitech4u

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.

Super User
Super User
Posts: 7,050

Re: Working with junk chars in SAS, while creating new variables

Posted in reply to munitech4u

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);
Solution
‎08-04-2016 03:46 AM
Super User
Posts: 5,505

Re: Working with junk chars in SAS, while creating new variables

[ Edited ]
Posted in reply to munitech4u

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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