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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2137 views
  • 7 likes
  • 3 in conversation