How to automatically define macro variables (names and values) based on a sas table contents

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

How to automatically define macro variables (names and values) based on a sas table contents

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?


Accepted Solutions
Solution
‎06-06-2018 11:26 AM
Super User
Posts: 23,771

Re: How to automatically define macro variables (names and values) based on a sas table contents

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.

View solution in original post


All Replies
Super User
Posts: 10,278

Re: How to automatically define macro variables (names and values) based on a sas table contents

Use call symput in the data step that creates the dataset:

call symput(varname,value);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 47

Re: How to automatically define macro variables (names and values) based on a sas table contents

Posted in reply to KurtBremser

@KurtBremser The table already exists, it's provided by the business. I put a data step in here for illustration purposes. 

Solution
‎06-06-2018 11:26 AM
Super User
Posts: 23,771

Re: How to automatically define macro variables (names and values) based on a sas table contents

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.

Contributor
Posts: 47

Re: How to automatically define macro variables (names and values) based on a sas table contents

So simple, it's embarrassing. 

Super User
Posts: 13,583

Re: How to automatically define macro variables (names and values) based on a sas table contents


@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 Smiley Embarassed

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 114 views
  • 3 likes
  • 4 in conversation