Help using Base SAS procedures

Counting the number of variables

Reply
Frequent Contributor
Frequent Contributor
Posts: 94

Counting the number of variables

Is there a way to count the number of variables in a dataset?
Thanks.
Valued Guide
Posts: 2,177

Re: Counting the number of variables

why?
Frequent Contributor
Frequent Contributor
Posts: 94

Re: Counting the number of variables

The output dataset of my code should have a defined number of variables (after various merge operations). I want to build an error check which displays the number of variables in the output data. Thanks.
Valued Guide
Posts: 2,177

Re: Counting the number of variables

after the step where table lib.your_table is wriitten, you can put the number of columns into a macro variable (&n_columns) with the syntax[pre]%let n_columns = none ;
proc sql noprint ;
select nVar
into :n_columns separated by ' '
from dictionary.tables
where libname= 'LIB'
& memname= 'YOUR_TABLE'
;
quit ;
%put your_table has &n_columns columns ;[/pre]If the table does not exist, the macro variable n_columns will hold "none".
Frequent Contributor
Frequent Contributor
Posts: 94

Re: Counting the number of variables

Many Thanks, Peter.
Occasional Contributor P_J
Occasional Contributor
Posts: 10

Re: Counting the number of variables

3078 data a;
3079 a = 10;
3080 label a = "number";
3081 b = vlabel(a);
3082
3083 run;

NOTE: The data set WORK.A has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds


3084
3085 data _null_;
3086 set sashelp.vtable;
3087 where libname ="WORK" AND memname = upcase("a");
3088 put "nu of var=" nvar;
3089 run;

nu of var=2
Frequent Contributor
Frequent Contributor
Posts: 94

Re: Counting the number of variables

Excellent solution! Thanks P.J.

V
Ask a Question
Discussion stats
  • 6 replies
  • 167 views
  • 0 likes
  • 3 in conversation