BookmarkSubscribeRSS Feed
wbguy
Fluorite | Level 6
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
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Ksharp
Super User
Obviously. You need another style of sum function.
[pre]
_sum=sum( of weight height age);
[/pre]


Ksharp
wbguy
Fluorite | Level 6
The sum function seems to be the simplest way to accomplish what I want......thanks a lot!!!
chang_y_chung_hotmail_com
Obsidian | Level 7
...
> 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]

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1561 views
  • 0 likes
  • 4 in conversation