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

If I create one variable list like this,

%let varlist=c1-c9;

How to get the number of variables in &varlist?

If I %let  n=%sysfunc(countw(&varlist)), &n will be 2.

Any way to get the exact number of variables in &varlist?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Second way :

%let varlist=c1-c9;

data _null_;
array a{*} &varlist;
call symputx('n',dim(a));
stop;
run;
%put &n ;

Ksharp

View solution in original post

6 REPLIES 6
Ksharp
Super User

How about :

%let varlist=c1-c9;

%let start=%scan(&varlist,1, ,kd);

%let end=%scan(&varlist,-1, ,kd);

%let n=%eval(&end-&start+1) ;

%put &n ;

Ksharp

Ksharp
Super User

Second way :

%let varlist=c1-c9;

data _null_;
array a{*} &varlist;
call symputx('n',dim(a));
stop;
run;
%put &n ;

Ksharp

MikeTurner
Calcite | Level 5

Thank you, Ksharp!

MikeTurner
Calcite | Level 5

Another question.

%let varlist=c1-c4 d1 d9;

How can I  get the variable name list like c1 c2 c3 c4 d1 d9?

Thanks!

data_null__
Jade | Level 19

proc transpose data=have(obs=0) out=varlist;

   var c1 c2 ..... ;  * any list of variables that exist in have;

   run;

You get a SAS data set and you can use it to answer all kinds of questions including how many.

Ksharp
Super User

Acutally NULL has already given you a good solution.

%let varlist=c1-c4 d1 d9;

data _null_;
length list $ 200;
array a{*} &varlist;
do i=1 to dim(a);
 list=catx(' ',list,vname(a{i}));
end;
call symputx('list',list);
stop;
run;
%put &list ;

Ksharp

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1534 views
  • 7 likes
  • 3 in conversation