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?
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).
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With