BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12
  • 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'? 
  • Variable y producing the values of variables  'id!!age!!sex' which is available in have dataset in the output. E.g.'00125M'
9 REPLIES 9
Astounding
PROC Star

You are vastly overcomplicating the problem.  SYMGET is not needed to retrieve the value of a macro variable.  Simply use:

 

x = &field_list;
David_Billa
Rhodochrosite | Level 12
I tried this method already and it is producing the same result as it
produce with symget function.
Astounding
PROC Star

Then you will need to show the log, as well as the results from this statement:

 

%put *&field_list*;

David_Billa
Rhodochrosite | Level 12
I don't have SAS at the moment to send you the log.But I remember that the
macro variable resolved to 'id!!age!!sex'
Tom
Super User Tom
Super User

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(&macrovar));

Then it is the same as using this statement:

x = id!!age!!sex ;
David_Billa
Rhodochrosite | Level 12
Macro variable don't have quotes in it. So can I tackle it with only
%sysfunc function by removing deqoute function per your post?
Tom
Super User Tom
Super User
If the macro variable does not have quotes then just expand the macro variable to generate the code.
x = &macrovar ;
Shmuel
Garnet | Level 18

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';

 

 

Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1086 views
  • 1 like
  • 4 in conversation