SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
vicmaria98
Calcite | Level 5
I currently have code with a bunch of %let statements which are then used to do calculations with my data. My goal is to create a macro that reads in a sas table and changes those %let value statements. Is there a way to assign %let to variable values in a table?
4 REPLIES 4
sbxkoenk
SAS Super FREQ

Run the below code to learn about an alternative:

data _NULL_;
 set sashelp.class;
 call symputx('NAME'!!strip(put(_N_,8.)),name);
run;
%PUT &=NAME1;
%PUT &=NAME2;
%PUT &=NAME3;
%PUT &=NAME19;

Cheers,

Koen

Reeza
Super User

%LET creates macro variables. 

There are many other ways to create macro variables from tables - CALL SYMPUTX() or PROC SQL are two ways. 

So let's look at creating macro variables for all the names in the CLASS data set, with a data step and a macro. 

 

proc sql noprint;
select name into :sql_name1-
from sashelp.class;
quit;

%put &sql_name1.;
%put &sql_name19.;

And via data step:

 

data _null_;
*for debugging;
*data demo;
set sashelp.class;

macro_variable_name = catt('dataStep_name', put(_n_, 2. -l));

call symputx(macro_variable_name, name);

run;

%put &dataStep_name1;
%put &dataStep_name19.;

@vicmaria98 wrote:
I currently have code with a bunch of %let statements which are then used to do calculations with my data. My goal is to create a macro that reads in a sas table and changes those %let value statements. Is there a way to assign %let to variable values in a table?

 

PaigeMiller
Diamond | Level 26

I currently have code with a bunch of %let statements which are then used to do calculations with my data.


It's much easier to do calculations in a DATA step than in macro variables.

 

My goal is to create a macro that reads in a sas table and changes those %let value statements. Is there a way to assign %let to variable values in a table?

So your data is in a SAS "table", do the calculations in a DATA step. Much much easier. Macros are likely unnecessary.

 

Why do you think macro variables will help here?

--
Paige Miller
andreas_lds
Jade | Level 19

Moving data into macro-variables for calculations is a bad idea in approximately 100% times. So please tell us what you are trying to. I am sure, that a solution can be developed that allows the data to stay in its natural living space.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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