The SAS Output Delivery System and reporting techniques

Export NLEVELS from PROC FREQ to a DataSet

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Export NLEVELS from PROC FREQ to a DataSet

Hi Guys

 

So this is the situation I have a bunch of tables that i need to bring all of their variables and pull all the different values they can hold so i made this

 

 

proc contents data=xxx out=yyy (keep=name); run;

 

proc sql noprint; select count(*) into : maxvar separated by 'anything' from yyy; quit;

 

%macro loop;

 

   %do i = 1 %to &maxvar.

 

      data _null_; set yyy; if _n_ = &i. then call symput ("var", compress(name)); run;

 

      proc freq data = yyy nlevels; tables &var. / noprint; run;

 

   %end;

 

%mend; %loop;

 

 

Im doing it one by one since there are some previous filters applied to the list of variables to pull their levels. If i do an out = zzz this will bring a list of all the values and their percentage, i dont need that i just need a dataset with the levels of the varariable so then i can concatenate them into a single dataset.

 

 


Accepted Solutions
Solution
‎07-11-2018 03:13 PM
Super User
Super User
Posts: 8,273

Re: Export NLEVELS from PROC FREQ to a DataSet

Posted in reply to Cynthia_sas

If you don't want to actually print the frequencies, just get the number of levels, then add the /NOPRINT option to the TABLES statement.

View solution in original post


All Replies
SAS Super FREQ
Posts: 9,426

Re: Export NLEVELS from PROC FREQ to a DataSet

Posted in reply to BernyOsuna

Hi:

  I'm not sure you need a macro process unless you want something more complicated than this. The name of the output object for NLEVELS is very sensibly named "NLEVELS" so you can use an ODS OUTPUT statement to get the NLEVELS for all the variables into 1 datastep with one PROC FREQ step:

ods output nlevels=work.nlev_all;
proc freq data=sashelp.shoes nlevels;
  tables _all_;
run;

proc print data=work.nlev_all;
run;

produces this:

nlevels_shoes.png

 

Of course, you might not want to get the levels on the numeric variables, in which case you could change the dataset to have

tables _character_;

If you just wanted character variables.

 

Cynthia

Solution
‎07-11-2018 03:13 PM
Super User
Super User
Posts: 8,273

Re: Export NLEVELS from PROC FREQ to a DataSet

Posted in reply to Cynthia_sas

If you don't want to actually print the frequencies, just get the number of levels, then add the /NOPRINT option to the TABLES statement.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 67 views
  • 0 likes
  • 3 in conversation