Data test(keep=x y);
Set have;
x=symget('field_list');
*macro variable field_list resolves to id!!age!!sex
y=id!!age!!sex;
run;
In the above code I except the variables x and y to produce the same value in the output but in the variable 'x' I'm getting values as 'id!!age!!sex' instead of the respective values of those variables. What I'm missing while I creating the variable 'x'? You are vastly overcomplicating the problem. SYMGET is not needed to retrieve the value of a macro variable. Simply use:
x = &field_list;
Then you will need to show the log, as well as the results from this statement:
%put *&field_list*;
If the macro variable has the quotes in it then you need to remove them.
So if you have:
%let macrovar='id!!age!!sex';
And you use this statement in a data step:
x = %sysfunc(dequote(¯ovar));
Then it is the same as using this statement:
x = id!!age!!sex ;
X will get the macro variable as on long string 'id!!age!!sex';
Y will concatenate the values of the 3 variables mentioned: ID, age, sex.
AS long those 3 variables exist in HAVE table you'll get different result, otherwise a warning.
To get the same result you should assign y='id!!age!!sex';
They should NOT produce the same value.
The first is getting the value of the macro variable as a character string.
The second is concatenating the values of three dataset variables.
If you want them to produce the same values then perhaps you don't want to use SYMGET() at all. Just expand the macro variable.
x=&field_list;
y=id!!age!!sex;
So to the data step that will be the same as if you typed:
x=id!!age!!sex;
y=id!!age!!sex;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.