Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SAS have an inline if function or ternary operator?

Tags:

sas

I'm trying to concatenate a long string in SAS and it would be helpful to have an inline if function or ternary operator so that I can nest IF statements in the concatenation. I can't find mention of this in the docs. In a DATA step, I'd like to do something like:

myString = "some words " || dead == 1 ? 't' : 'f' || " some more words" ....

Basically, I'm trying to generate some seeds for demonstration Rails app, so that I can dump some SAS data into a SQLite database quickly.

Is there any sort of inline if in SAS?

like image 228
Clay Avatar asked Jul 28 '13 02:07

Clay


2 Answers

The ifc function (character version, ifn numeric) is the inline if function in SAS. That in SAS would be:

myString = cat("some words ",ifc(dead=1,'t','f')," some more words");

(cat family functions like cat,catx,etc. are more commonly used than the || operator in SAS).

like image 78
Joe Avatar answered Oct 19 '22 22:10

Joe


A more traditional SAS way to generate text based on the value of a variable is to define a format.

proc format ;
  value dead 1='dead' 0='alive' other='unknown';
run;
...
myString = catx(' ','some words',put(dead,dead.),'some more words');
like image 1
Tom Avatar answered Oct 19 '22 22:10

Tom