philihpAboutPGPLightning

Using SUBSTR on the left of an assignment in SAS

Philihp Busby,1 min read

This is a pretty cool feature, and as far as I know it’s unique to SAS and Perl. In SAS, you can have a substr to the left of an assignment .

stringDate='29MAR1984'; substr(stringDate,3,3)='FEB';

This avoids potentially awkward situations like this:

stringDate='29MAR1984'; stringDate=substr(stringDate,1,2)||'FEB'||substr(stringDate,6,4);

The bounds don’t have to be static. Lets suppose we have a dataset with 1 row per letter, and we want to combine those letters into a word.

data have; input letter $1.; datalines; h e l l o ; run;

My initial thought was to just retain and append each letter:

data want; length word $200; retain word; set have nobs=nobs; call cats(word,letter); if(nobs=_n_) then output; run;

However this breaks if one of the letters is a space, and SAS treats the space as null, so appending a space has no effect on the string. Instead of appending, just stick the letter where it’s supposed to be.

data want; length word $200; retain word ' '; set have nobs=nobs; substr(word,_n_,1) = letter; if(_n_==nobs) then output; run;

GitHub · Bluesky · LinkedIn · Instagram · KeybaseRSS

Built from 8f0906ac CC BY 4.0 — with love from San Francisco.