BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Elle
Quartz | Level 8

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

5 REPLIES 5
Elle
Quartz | Level 8

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

Reeza
Super User
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.

ballardw
Super User

@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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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