DATA Step, Macro, Functions and more

Manipulating a macro variable list

Reply
Occasional Contributor
Posts: 5

Manipulating a macro variable list

Hi all,

I have a macro [ %reg] where I have an argument [varlist] which is a variable list. This will contain variable names, delimited by a blank or blanks, and the total number of variable names passed to varlist itself can vary.

A simple version of my code looks like this:

----------------------------------------------------------------------------
data a;
input y x1 x2 x3 x4_old x4_new;
datalines;
1 8 7 2 4 7
3 4 6 2 7 2
9 0 2 1 7 -9
9 5 3 8 7 0
0 9 8 3 5 -7
;
run;


options symbolgen;
%macro lin(method,varlist);
proc reg data =a;
model y = &varlist/&method;
run;
%let varcnt=%sysfunc(countw(&varlist));
%put There are &varcnt variables in the model;
data _null_;
set a;
%do i=1 %to &varcnt;
%let var&i =%qscan(&varlist,&i);
%put var&i = &&var&i;
%end;
run;
%mend lin;
--------------------------------------------------------------------------

%lin(all, x1 x2 x4_old); > invoking the macro in this way gives desired results.

Now, I want to put in a sum statement like the following :

z=sum( all the variables that are in varlist).

eg z = sum(x1,x2,x4_old) ;

Obviously, I need a comma seperated variable list. Can any one please show me the quickest way to do this? Thanks a lot for your help!!

Best, wbguy
Super Contributor
Super Contributor
Posts: 3,174

Re: Manipulating a macro variable list

Use the TRANSPOSE function and do it in macro code with also using the %SYSFUNC(....) macro call function in a %LET statement.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

sysfunc data step using function call site:sas.com
Super User
Posts: 9,662

Re: Manipulating a macro variable list

Obviously. You need another style of sum function.
[pre]
_sum=sum( of weight height age);
[/pre]


Ksharp
Occasional Contributor
Posts: 5

Re: Manipulating a macro variable list

The sum function seems to be the simplest way to accomplish what I want......thanks a lot!!!
Regular Contributor
Posts: 241

Re: Manipulating a macro variable list

...
> Obviously, I need a comma seperated variable list.
> Can any one please show me the quickest way to do
> this? Thanks a lot for your help!!
...
Here is a simple macro that converts input blank separated list into a comma separated list. Hope this helps a bit.
[pre]
%macro b2c(src);
%local comma blank;
%let comma = %str(,);
%let blank = %str( );
%let src = %sysfunc(strip(&src));
%sysfunc(translate(&src,&comma,&blank))
%mend b2c;

%put ***%b2c(x y z)***;
%*-- on log
***x,y,z***
--*;
[/pre]
Ask a Question
Discussion stats
  • 4 replies
  • 152 views
  • 0 likes
  • 4 in conversation