SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Converting list of macro variables into dataset variables

Reply
Contributor
Posts: 22

Converting list of macro variables into dataset variables

I have a list of macro variables as shown below:

%let a=40;

%let b=55;

%let c=25;

%let d=38;

%let e = 41 and so on.

I would like to convert this to a sas dataset in the manner of

a     b       c     d     e

40   55    25   38   41

 

I could not find anything on a quick way to do this. All I can think of is writing a data step with datalines to create the dataset. Is there a quicker way to do it, given I have large number of variables? Thank you!

Highlighted
PROC Star
Posts: 1,569

Re: Converting list of macro variables into dataset variables

%let a=40;

%let b=55;

%let c=25;

%let d=38;

%let e = 41;

data w;
a=&a;
b=&b;
c=&c;
d=&d;
e=&e;
run;
Super User
Posts: 6,629

Re: Converting list of macro variables into dataset variables

[ Edited ]

Are all your values numeric?  

 

Can you use all existing macro variables without omitting any?

 

You should be able to do something like this, but I can't test this now so I can't work out the final details until tomorrow:

 

proc sql;

select name || '=' || value  into : varlist separated by ';' from dictionary.macros

where scope ne 'AUTOMATIC';

quit;

data want;

&varlist;

run;

 

There are a few details to work out ... SCOPE is probably the wrong variable name for the WHERE clause ... in fact all the names from dictionary.macros need to be checked  To patch the code, you will need to look at the structure of dictionary.macros.

 

 

***************** EDITED:

 

The original program wasn't too far off.  Here's a modified version that should work a little better:

 

proc sql;

   select catx('=', name, value) into : varlist separated by ';' from dictionary.macros

   where (scope ne 'AUTOMATIC') and (substr(name, 1, 3) not in ('SQL', 'SYS') );

quit;

 

It generates assignment statements for all your macro variables, however ...

 

It works properly for numeric variables only, since it generates statements like  a=5;  It won't add quotes around the "5" unless you get fancier with the code.

 

Super User
Posts: 9,880

Re: Converting list of macro variables into dataset variables

From where does this arbitrary heap of macro variables originate? You might alleviate your problems by storing the values in a dataset from the start.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 3 replies
  • 132 views
  • 0 likes
  • 4 in conversation