Help using Base SAS procedures

Select Variables by Column Number Rather Than Name

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Select Variables by Column Number Rather Than Name

Hello SAS Community,

Does anyone know a way to call a variable based on the column location rather than the column name?

I would like to pass data sets to a macro procedure and run FREQ on the first 2 columns of the data set, however the names of these variables will vary. So my question is if I can identify the variables in the FREQ procedure without knowing the variable names.

PROC FREQ DATA =T1;

TABLE ColVar1 /OUT =T2;

TABLE ColVar2 /OUT =T3;

quit;

Thank you for your help.

Mike


Accepted Solutions
Solution
‎10-21-2014 03:32 PM
Respected Advisor
Posts: 3,799

Re: Select Variables by Column Number Rather Than Name

Posted in reply to mikemangini

You would be better off with one data set of all the variables in one. ODS OUTPUT ONEWAYFREQS

%macro freqbynum(data=,num=2);
  
%local varlist;
   proc transpose data=&data(obs=0) out=varnames;
      var _all_;
      run;
   proc sql noprint;
      select catx(
' ','tables',_name_,cats('/ out=T',monotonic()),';') into :varlist separated by ' '
         from varnames(obs=&num);
      quit;
      run;
   proc freq data=&data;
      &varlist;
      run;
  
%mend;
options mprint=1;
%
freqbynum(data=sashelp.heart);

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Select Variables by Column Number Rather Than Name

Posted in reply to mikemangini

This?

%macro freqbynum(data=,num=2);
  
%local varlist;
   proc transpose data=&data(obs=0) out=varnames;
      var _all_;
      run;
   proc sql noprint;
      select _name_ into :varlist separated by
' '
         from varnames(obs=&num);
      quit;
      run;
   proc freq data=&data;
      tables &varlist;
      run;
  
%mend;
%
freqbynum(data=sashelp.heart);
Contributor
Posts: 34

Re: Select Variables by Column Number Rather Than Name

Posted in reply to data_null__

This almost does what I was hoping perfectly. One last step though is that I would like to put the output from each variable out into its own table. Any ideas?

TABLE ColVar1 /OUT =T2;

TABLE ColVar2 /OUT =T3;

Solution
‎10-21-2014 03:32 PM
Respected Advisor
Posts: 3,799

Re: Select Variables by Column Number Rather Than Name

Posted in reply to mikemangini

You would be better off with one data set of all the variables in one. ODS OUTPUT ONEWAYFREQS

%macro freqbynum(data=,num=2);
  
%local varlist;
   proc transpose data=&data(obs=0) out=varnames;
      var _all_;
      run;
   proc sql noprint;
      select catx(
' ','tables',_name_,cats('/ out=T',monotonic()),';') into :varlist separated by ' '
         from varnames(obs=&num);
      quit;
      run;
   proc freq data=&data;
      &varlist;
      run;
  
%mend;
options mprint=1;
%
freqbynum(data=sashelp.heart);
Contributor
Posts: 34

Re: Select Variables by Column Number Rather Than Name

Posted in reply to data_null__

Thank you so much for your solution. I will try ODS.

Super User
Posts: 10,018

Re: Select Variables by Column Number Rather Than Name

Posted in reply to data_null__

NULL , You can do it as simple as :

data _null_;
 dsid=open('sashelp.class','i');
 call symputx('first',varname(dsid,1));
 call symputx('second',varname(dsid,2));
run;
%put First is: &first | Second is: &second ;
proc freq data=sashelp.class;
table &first ;
table &second ;
run;

Xia Keshan

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 2959 views
  • 4 likes
  • 3 in conversation