I've tried various ways to do this but I'm stumped. I want to use a variable in data _null_ to make a decision. Something like this
%let clname = eaton;
data _null_;
if &clname. = bob then put '5xxxx';
else put 'yyyy';
run;
Thanks.
But you don't have a variable with the named CLNAME. In fact you did not create any variables in your code (other than accidentally by referencing the variables EATON and BOB that hadn't been defined yet).
You created a MACRO VARIABLE with that name. To test the value of a macro variable in SAS code (as opposed to macro code) you need to either resolve it inside of double quotes.
if "&clname" = 'bob' then put '5xxxx';
Or use the SYMGET() function. The SYMGET() function wants a character value that is the name of the macro variable.
if symget('clname') = 'bob' then put '5xxxx';
You would normally not need to use that as the value of CLNAME will not normally change while the data step is running. If it did change (for example because the data step had earlier used the CALL SYMPUTX() function) then it might make sense. Or if the value of CLNAME is something that would confuse SAS when it tried to evaluate "&clname".
You have to understand how macro variables work. When you run the program, &clname is replaced by its value, eaton. That's all it does in this case. And whatever results must be legal valid working SAS code.
So, in your case, the line of code with &clname, when executed, is:
if eaton = bob then put '5xxxx';
Is this what you want? Can you spot a possible problem here? Is this legal valid WORKING code?
Can you explain, in words, what you want this code to do? Because really, if we're going to help here, we need to know what you want this code to do.
@PaigeMiller wrote:
You have to understand how macro variables work. When you run the program, &clname is replaced by its value, eaton. That's all it does in this case. And whatever results must be legal valid working SAS code.
So, in your case, the line of code with &clname, when executed, is:
if eaton = bob then put '5xxxx';
Is this what you want? Can you spot a possible problem here? Is this legal valid WORKING code?
Can you explain, in words, what you want this code to do? Because really, if we're going to help here, we need to know what you want this code to do.
If the variable clname resolves to bob I want to print "5xxxx", otherwise I want to print "yyyy"
@DanD999 wrote:
If the variable clname resolves to bob I want to print "5xxxx", otherwise I want to print "yyyy"
HINT: before you try to write code with macro variables, you need to get the code to work without macro variables, in other words, make this code work with purely DATA step code. I don't think you have done that; and if you are able to do that, then getting the macro to work is easy. And if you can't get the DATA step code to work, then you will probably never get the macro code to work. Can you show us working data step code with no macro variables that does what you want?
Why doesn't your code work?
Clname is a macro variable with the value eaton. When this macro variable is resolved in a data step, eaton is the name of a variable which apparently doesn't exist. also, bob is the name of a variable that doesn't exist. So you are comparing variable eaton, which doesn't exist, to variable bob, which doesn't exist. That doesn't sound like what you want.
Perhaps you want to compare the text string "eaton" to the text string "bob". If this is what you want to do (is it?), you should be able to make some extremely minor changes to your code to do this string comparison.
@DanD999 wrote:
@PaigeMiller wrote:
You have to understand how macro variables work. When you run the program, &clname is replaced by its value, eaton. That's all it does in this case. And whatever results must be legal valid working SAS code.
So, in your case, the line of code with &clname, when executed, is:
if eaton = bob then put '5xxxx';
Is this what you want? Can you spot a possible problem here? Is this legal valid WORKING code?
Can you explain, in words, what you want this code to do? Because really, if we're going to help here, we need to know what you want this code to do.
If the variable clname resolves to bob I want to print "5xxxx", otherwise I want to print "yyyy"
I think you're making this more complicated than it is.
I'm setting a variable outside of the data _null_ to a value, in this case "eaton". I want to use that variable inside of the data _null_ to make a decision. How do I get the data _null_ to recognize that I've set the variable "clname" to "eaton" on the outside and use it on the inside?
But you don't have a variable with the named CLNAME. In fact you did not create any variables in your code (other than accidentally by referencing the variables EATON and BOB that hadn't been defined yet).
You created a MACRO VARIABLE with that name. To test the value of a macro variable in SAS code (as opposed to macro code) you need to either resolve it inside of double quotes.
if "&clname" = 'bob' then put '5xxxx';
Or use the SYMGET() function. The SYMGET() function wants a character value that is the name of the macro variable.
if symget('clname') = 'bob' then put '5xxxx';
You would normally not need to use that as the value of CLNAME will not normally change while the data step is running. If it did change (for example because the data step had earlier used the CALL SYMPUTX() function) then it might make sense. Or if the value of CLNAME is something that would confuse SAS when it tried to evaluate "&clname".
@Tom wrote:
But you don't have a variable with the named CLNAME. In fact you did not create any variables in your code (other than accidentally by referencing the variables EATON and BOB that hadn't been defined yet).
You created a MACRO VARIABLE with that name. To test the value of a macro variable in SAS code (as opposed to macro code) you need to either resolve it inside of double quotes.
if "&clname" = 'bob' then put '5xxxx';
Or use the SYMGET() function. The SYMGET() function wants a character value that is the name of the macro variable.
if symget('clname') = 'bob' then put '5xxxx';
You would normally not need to use that as the value of CLNAME will not normally change while the data step is running. If it did change (for example because the data step had earlier used the CALL SYMPUTX() function) then it might make sense. Or if the value of CLNAME is something that would confuse SAS when it tried to evaluate "&clname".
Thanks Tom.
Symget is the way to get the value of the variable on the outside of the data _null_ into the data _null_
data _null_;
if symget('client') = 'eaton' then call symputx('start_dt', put(intnx('month',&sas_run_date.,-11,'beg'),yymmddn8.));
else call symputx('start_dt',"'"|| put(&sas_beg_dt.,date9.) ||"'");
run;
%put ===> start_dt = &start_dt;
Thank you Quentin and PaigeMiller for responding.
A macro variable (or macro symbol) is NOT A VARIABLE.
Can you describe more about what your are trying to do, or what results your are expecting?
Your code runs without errors, the log is:
1 %let clname = eaton; 2 3 data _null_; 4 5 if &clname. = bob then put '5xxxx'; 6 7 else put 'yyyy'; 8 9 run; NOTE: Variable eaton is uninitialized. NOTE: Variable bob is uninitialized. 5xxxx
Since there is no variable named EATON or BOB, they are created as numeric variables with missing values, and the comparison is true.
Did you perhaps want to compare the string "eaton" to the string "bob"? If so, you need to add quotes around the string values, so that SAS knows they are not variable names:
10 %let clname = eaton; 11 12 data _null_; 13 14 if "&clname." = "bob" then put '5xxxx'; 15 16 else put 'yyyy'; 17 18 run; yyyy
@Quentin wrote:
Can you describe more about what your are trying to do, or what results your are expecting?
Your code runs without errors, the log is:
1 %let clname = eaton; 2 3 data _null_; 4 5 if &clname. = bob then put '5xxxx'; 6 7 else put 'yyyy'; 8 9 run; NOTE: Variable eaton is uninitialized. NOTE: Variable bob is uninitialized. 5xxxxSince there is no variable named EATON or BOB, they are created as numeric variables with missing values, and the comparison is true.
Did you perhaps want to compare the string "eaton" to the string "bob"? If so, you need to add quotes around the string values, so that SAS knows they are not variable names:
10 %let clname = eaton; 11 12 data _null_; 13 14 if "&clname." = "bob" then put '5xxxx'; 15 16 else put 'yyyy'; 17 18 run; yyyy
I want to have a variable that I can set to a value, in case "eaton" and then inside the data _null_ I want to evaluate that variable and if it is "eaton", I'll do one thing and if it's not "eaton" I'll do something else.
Macro variables resolve before the data step executes. So from a SAS data step perspective you're just dealing with a string.
Strings need to be in quotes. AND for macro variables to resolve they need to be in double quotes.
%let clname = eaton;
data _null_;
if "%upcase(&clname)" = "BOB" then put 'xxxx';
else if "%upcase(&clname)" = "EATON" then put 'yyyy';
else put 'zzzz';
run;
Thanks for an alternate way Patrick.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.