Help using Base SAS procedures

How to get the number of variables in one list?

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

How to get the number of variables in one list?

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!


Accepted Solutions
Solution
‎05-18-2012 05:21 AM
Super User
Posts: 10,028

Re: How to get the number of variables in one list?

Posted in reply to MikeTurner

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


All Replies
Super User
Posts: 10,028

Re: How to get the number of variables in one list?

Posted in reply to MikeTurner

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

Solution
‎05-18-2012 05:21 AM
Super User
Posts: 10,028

Re: How to get the number of variables in one list?

Posted in reply to MikeTurner

Second way :

%let varlist=c1-c9;

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

Ksharp

Contributor
Posts: 49

Re: How to get the number of variables in one list?

Thank you, Ksharp!

Contributor
Posts: 49

Re: How to get the number of variables in one list?

Another question.

%let varlist=c1-c4 d1 d9;

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

Thanks!

Respected Advisor
Posts: 3,799

Re: How to get the number of variables in one list?

Posted in reply to MikeTurner

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.

Super User
Posts: 10,028

Re: How to get the number of variables in one list?

Posted in reply to MikeTurner

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 210 views
  • 7 likes
  • 3 in conversation