SAS Programming

DATA Step, Macro, Functions and more
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
Onyx | Level 15

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.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 3883 views
  • 5 likes
  • 5 in conversation