BookmarkSubscribeRSS Feed
vicmaria98
Calcite | Level 5

Is there a way to do something like the below?

 

%let x = &Table.x;

 

I want to assign the macro variable to the value that is found in the Table. I cannot use proc sql no print as I am doing this in a datastep.

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Use the Call Symputx Routine

 

like this

 

data have;
   x = 100;
   
   call symputx('x', x);
run;

%put &x.;
yabwon
Amethyst | Level 16

I'm just fooling around but you could "resolve" this problem with %let statement too 😉 😉

 

data table;
  x = 42;
run;

data _null_;
  set table;
  rc = resolve('%let x = ' || put(x, best32.) || ';');
run;

%put &=x.;

 

 

B-)

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

@vicmaria98 wrote:

Is there a way to do something like the below?

 

%let x = &Table.x;

 

I want to assign the macro variable to the value that is found in the Table. I cannot use proc sql no print as I am doing this in a datastep.


Use the CALL SYMPUTX() function to create a macro variable from a data step. 

data _null_;
  set table;
  call symputx('x',x);
run;

But remember that if you need to use that macro variable in the same data step you will need to use the SYMGET() or SYMGETN() to retrieve the value.  If you just reference the macro variable with &X that reference will be resolved BEFORE the data step is compiled and so definitely before the CALL SYMPUTX() function call can execute.

 

If you do need to use the value within the same data step then just leave the value in a data step variable instead of converting it to text to store into a macro variable and then back from text to a variable.  Note that you could use a variable that is not written to the output dataset(s).  If you need to prevent the data step from setting the variable to missing when it starts its next iteration then use the RETAIN statement.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 4222 views
  • 5 likes
  • 5 in conversation