BookmarkSubscribeRSS Feed
robertrao
Quartz | Level 8

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

 

 

4 REPLIES 4
Astounding
PROC Star

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;

thomp7050
Pyrite | Level 9

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

Kurt_Bremser
Super User

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.

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
  • 4 replies
  • 788 views
  • 1 like
  • 5 in conversation