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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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