Multiple Assignments or the Idiosyncrasies of R and SAS

May 8, 2008
By

(This article was first published on fernandohrosa.com.br - en » R, and kindly contributed to R-bloggers)

Working on top of someone else’s code at work a couple of weeks ago I stumbled upon a piece of SAS code that went like this:

1
2
3
  length check 3;
  ...
  check = Name = 'Netter'

Being raised in the land of open-source and having programmed in R since I was in college, that struck me at first as an implicit cast error, as check was previously declared integer. Or a misguided attempt to create an indicator variable. What surprised me even more was that not only there was no cast error as well as the code ran and gave a sensible result in the context of the program I was editing.

If you still did not spot the weirdness of the statement above, try running the exact same piece of code in R (parenthesis added on purpose to return the result of the assignment):

View Code RSPLUS
1
2
3
4
5
6
 R > ( check = Name = 'Netter' )
 [1] "Netter"
 R > str(check)
    chr "Netter"
 R > Name
 [1] "Netter"

Now the explanation: due to the way assignments and comparison operators are defined across both languages, the same code in R will result in a Multiple Assignment, erasing the previous value of check and writing a character string over it, the same with Name, which will also be set to character.

In SAS, where the = operator does assignments as well as comparisons (though one could argue that you could use the ‘eq’ mnemonic instead) the same expression evaluates if the CHAR variable Name is equal to the constant ‘Netter’, and the result of this logical expression (0 or 1) is then attributed to the check (NUMERIC) variable.

Related references:

Venables, W.N, Ripley, B. D. (2000) S Programming.
Slaughter, S.J., Delwiche, L. D. (2004) Little Sas Book.

Code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
data sample;
    input id nome $;
    datalines;
1     Netter
2     Nelder
3     McCulough
;
run;
 
data sample;
    set sample;
    chk = nome = 'Netter';
    run;

flattr this!

To leave a comment for the author, please follow the link and comment on his blog: fernandohrosa.com.br - en » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.