DATA Step, Macro, Functions and more

Blanks

Reply
Super Contributor
Posts: 1,041

Blanks

Hello ,

 

Is there any preference for using "" versus " " to identify blank charecter fields?

 

Example:

I am trying to assign a value of 'OP' to all the ID's which are blank

so the IF statement can be either of the 2 below?

 

if INP_OP=" " then INP_OP='OP';  ---1 space

if INP_OP="" then INP_OP='OP'; ----no space between quotes

 

INP_OP    ID

               101

INP         102

INP         103

               104

INP         105

INP         106

               107

              108
INP        109

 

 

Super User
Posts: 5,503

Re: Blanks

Posted in reply to robertrao

For the examples you listed, pick whichever you like.  I find it easier to read the code when the blank appears.

 

Technically, there are other cases where it might make a difference.  Within PROC SQL, these produce different results:

 

separated by ' '

separated by ''

 

I haven't tested it, but I imagine these could produce different results as well:

 

if in_op =: ' ' then do;

if in_op =: '' then do;

Frequent Contributor
Posts: 93

Re: Blanks

Posted in reply to robertrao

Out of curiosity, which one is more accurate?  I assume that the second option would be more accurate, but what are you finding in your data?

Super User
Super User
Posts: 7,946

Re: Blanks

Posted in reply to robertrao

Tricky one.  Yes, most of the time that would be fine having "" or " ", my preference would be for the "" as that shows a null string, whereas the other looks almost like your expecting to be one space.  There are times where spaces are important, strip() versus trim() is one that pops to mind, and asis=on in proc report.  

If you want to be fool proof then the simplest method is to use the missing() function rather than the logic, so rather than:

if variable=" " then flag="Y";

Do:

if missing(variable) then flag="Y";

Of course this is easier if you use numeric flags:

flag=missing(variable);

Its a bit of two of one and half a dozen of the other though much like the do you use || or the cat functions, in most scenarios cat functions are more readable, is cat(a,b) better then a||b?

Super User
Posts: 7,778

Re: Blanks

Posted in reply to robertrao

When comparing a string literal to a character variable, "" is equivalent to " ", because a missing character variable is always filled with blanks, and the minimum length for a character variable is 1.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 131 views
  • 1 like
  • 5 in conversation