boot-scootin
boot-scootin

Reputation: 12515

Setting a default value for %sysget return value

I have the following code:

%let my_var = %sysget(MY_VAR);
%put &=my_var;

If I run the following code, my_var will be defined and print:

#!/bin/bash

export MY_VAR=1;
/sas/scripts/sas my_script.sas

But without that export statement, my_var is effectively not defined, and the script fully executes, without error.

#!/bin/bash

/sas/scripts/sas my_script.sas

I want to set a default value for what's returned from %sysget in case the variable isn't defined. How can I do that?

Upvotes: 1

Views: 261

Answers (1)

Tom
Tom

Reputation: 51601

You can use the ENVLEN() function to find the length of an environment variable. It will return -1 when it is not defined.

%let vname=MY_VAR;
%let want=DEFAULT;
%if %sysfunc(envlen(&vname))>-1 %then %do;
  %let want=%sysget(&vname.);
%end;
%put &=vname &=want;

Upvotes: 3

Related Questions