- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.