Hello,
I have a sas table looking like this:
data table;
input environment:$5. varname:$10. value:$100.;
datalines;
prod sanroot /data/sasdata/prod
prod macropath /data/sasdata/prod/macros
;run;
I need an ingenious way of defining an interactive list of macro variables, where for each row in the above table, 'varname' represents the macro's name and 'value' the macro's contents, like this:
%let sanroot=/data/sasdata/prod
%let macropath=/data/sasdata/prod/macros
Please ignore column 'environment' - there's a previous condition where I filter the table by environment.
I already have a function determining the nobs of the table.
Is there a magic function that can do this?
PS: I'm aware there are other methods - reading all values for 'varname' in a list using proc sql with into: and then do a loop.
I am looking for an intelligent alternative of a a few line(s) of code. I'm wondering if it's possible.
Basically I'm looking for something like this:
%let dsid=%sysfunc(open(table));
%do i=1 %to &nobs;
%let %sysfunc(getvarc(&dsid.,2))=%sysfunc(getvarc(&dsid.,3));
%end;
Any ideas?
data _null_;
set sashelp.class;
call symputx(name, age);
run;
%put &Alfred;
%put &Fred;
CALL SYMPUTX - X trims spaces and you can set it up as local/global.
Use call symput in the data step that creates the dataset:
call symput(varname,value);
@Kurt_Bremser The table already exists, it's provided by the business. I put a data step in here for illustration purposes.
data _null_;
set sashelp.class;
call symputx(name, age);
run;
%put &Alfred;
%put &Fred;
CALL SYMPUTX - X trims spaces and you can set it up as local/global.
So simple, it's embarrassing.
@Elle wrote:
So simple, it's embarrassing.
This may be obvious but you really want to make sure with an approach like this that the variable holding the macro variable name is unique even considering case: "name" "Name" "nAme" would all have the value sent to the same macro variable and you could spend a fair amount of time debugging values when only 10 actual different macro variables are created when you expect 11 (or 15 or ...).
And not to mention accidently using a name that would duplicate an already existing macro variable and replacing it's value.
Please don't ask me how I know
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.