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
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;
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?
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?
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.
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.
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.