BookmarkSubscribeRSS Feed
Mike7085
Calcite | Level 5

I'm doing data cleansing on source data. The rule is if attribute (text) = space, attribute = 'N'.   How do I code SAS so it doesn't intrepret ' ' as null?

4 REPLIES 4
art297
Opal | Level 21

Mike,

You probably will have to provide an example.  As you can see, with the following, you can always identify characters according to their actual byte value.  A space would be a 32, while a null would be a zero.  e.g.:

data _null_;

  file "c:\art\fortest.txt";

  put "Now is the time";

  put "The time was then";

run;

data test;

  infile "c:\art\fortest.txt";

  input;

  do i=1 to length(_infile_);

    x=substr(_infile_,i,1);

    y=rank(x);

    put y;

  end;

run;

Mike7085
Calcite | Level 5


if I enter the following code, :

IF FLDA = ' ' THEN FLDA = 'N' ;

SAS interprets this as ==>  if flda is null then flda = 'N'  what I want to do is put in a command so SAS interprets it as

if flda = space then flda = 'N'

Patrick
Opal | Level 21

In the SAS world a blank is treated as NULL. There is no specific NULL value and a character variable has also always a length of at least '1'.

For your requirement to work you would have to replace these "blanks" with 'N' before you map a SAS variable against it - or if you read the data from an external file you could determine the blanks by checking how much the pointer has moved for reading the next value as done in sample code below.


filename source temp;
data _null_;
  file source;
  put "Blank, ,it is";
  put "NULL,,it is";
  put "it is,blank, ";
  put "it is,NULL,";
  put;
  put " ";
  put ", ";
  put "   ,";
run;

data test(drop=_:);
  infile source dsd truncover col=CurCol dlm=',';

  _PrevCol=1;
  input a:$10. @;
  if _PrevCol+1>=CurCol then a='N';

  _PrevCol=CurCol;
  input b:$10. @;
  if _PrevCol+1>=CurCol then b='N';

  _PrevCol=CurCol;
  input c:$10. ;
  if _PrevCol+1>=CurCol then c='N';

run;

Tom
Super User Tom
Super User

SAS character variables are fixed length and padded on the right with spaces. So there is no concept of a NULL character variable.

If you are talking about testing input from a text file or a macro variable then you might be able to distinguish between a field with nothing in it versus a field with a space in it.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 616 views
  • 0 likes
  • 4 in conversation