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]

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
  • 4 replies
  • 868 views
  • 0 likes
  • 4 in conversation