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.
To set a macro variable from dataset values, you either need PROC SQL with SELECT INTO, or a DATA step with CALL SYMPUT/CALL SYMPUTX.
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-)
@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.
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!
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.
Ready to level-up your skills? Choose your own adventure.