BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bokola
Calcite | Level 5

I have the following macro from sas macro reference

 

%macro test(x);
%let x=1.5;
%do %while (&x<=2);
%put the value of x is &x;
%let x=%sysevalf(&x+0.25);
%end;

%mend test;
%test(1);

I wish to save the value of &x to a new variable say y. How can I achieve that?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It is hard to tell from your example code what your actual question is.  There is no data step code shown so no data step variable to save a value into.

 

Let's address the dataset variable issue first.

The easiest way to get a macro variable value into a data step is to just expand the value of the macro variable where you wants its text used to generate SAS code.

data want;
   myvar = &x ;
run;

So if macro variable X has values like 1.75 then the macro processor will generate SAS code like this:

data want;
   myvar = 1.75 ;
run;

Another possible interpretation of your question is around the use of macro variables and macros.  In your example code the macro variable X is defined as LOCAL to the macro TEST.  So when TEST ends X will disappear as there is no longer any symbol table to store it in.  If you want the calculated value of X to live after the macro finishes then assign its value to a macro variable that will exist.  One that is either GLOBAL or lives in the symbol table of a macro that is calling %TEST.

%macro test(x);
%do %while (%sysevalf(&x<=2));
  %put the value of x is &x;
  %let x=%sysevalf(&x+0.25);
%end;
%if not %symexist(y) %then %global y;
%let y=&x;
%mend test;

Another possible interpretation is to return the value of X as the result of calling the macro. Essentially convert TEST into a macro function.  This can only work if the macro only generates macro statements.  Do do that just expand the value to be returned in the macro definition.  And then call the macro as part of another statement.

%macro test(x);
%do %while (%sysevalf(&x<=2));
  %put the value of x is &x;
  %let x=%sysevalf(&x+0.25);
%end;
&x.
%mend test;
data want;
  myvar=%test(1);
run;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

It is hard to tell from your example code what your actual question is.  There is no data step code shown so no data step variable to save a value into.

 

Let's address the dataset variable issue first.

The easiest way to get a macro variable value into a data step is to just expand the value of the macro variable where you wants its text used to generate SAS code.

data want;
   myvar = &x ;
run;

So if macro variable X has values like 1.75 then the macro processor will generate SAS code like this:

data want;
   myvar = 1.75 ;
run;

Another possible interpretation of your question is around the use of macro variables and macros.  In your example code the macro variable X is defined as LOCAL to the macro TEST.  So when TEST ends X will disappear as there is no longer any symbol table to store it in.  If you want the calculated value of X to live after the macro finishes then assign its value to a macro variable that will exist.  One that is either GLOBAL or lives in the symbol table of a macro that is calling %TEST.

%macro test(x);
%do %while (%sysevalf(&x<=2));
  %put the value of x is &x;
  %let x=%sysevalf(&x+0.25);
%end;
%if not %symexist(y) %then %global y;
%let y=&x;
%mend test;

Another possible interpretation is to return the value of X as the result of calling the macro. Essentially convert TEST into a macro function.  This can only work if the macro only generates macro statements.  Do do that just expand the value to be returned in the macro definition.  And then call the macro as part of another statement.

%macro test(x);
%do %while (%sysevalf(&x<=2));
  %put the value of x is &x;
  %let x=%sysevalf(&x+0.25);
%end;
&x.
%mend test;
data want;
  myvar=%test(1);
run;

 

bokola
Calcite | Level 5
Hi Tom, thanks a lot. Achieves what I wanted.

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
  • 2 replies
  • 1219 views
  • 0 likes
  • 2 in conversation