BookmarkSubscribeRSS Feed
ManitobaMoose
Quartz | Level 8

Could someone provide a little more color. I know that it is an option in proc freq, but i am not really clear on its usefulness and what exactly it is. Thanks.

8 REPLIES 8
Reeza
Super User

Documentation is pretty thorough here IMO:

 

NLEVELS

displays the "Number of Variable Levels" table, which provides the number of levels for each variable named in the TABLES statements. For more information, see the section Number of Variable Levels Table. PROC FREQ determines the variable levels from the formatted variable values, as described in the section Grouping with Formats.

 

Source

 

Number of Variable Levels Table

If you specify the NLEVELS option in the PROC FREQ statement, PROC FREQ displays the "Number of Variable Levels" table. This table provides the number of levels for all variables named in the TABLES statements. PROC FREQ determines the variable levels from the formatted variable values. For more information, see the section Grouping with Formats. The "Number of Variable Levels" table contains the following information:

  • Variable name

  • Levels, which is the total number of levels of the variable

  • Number of Nonmissing Levels, if there are missing levels for any of the variables

  • Number of Missing Levels, if there are missing levels for any of the variables

Source

 

 

Translation:

It gives you the number of unique levels of a variable, for example in SASHELP.CLASS, the variable SEX has 2 different unique values, while Age has XXX. 

 

 

MikeFox
Fluorite | Level 6

I think the documentation here is lacking. What is really needed is a definition of the word "levels": one might infere it but, since it's quite easy to come up with a robust one, it should be added into the official documentation. For example "A level of a variable is one of the unique values that such variable assumes in the dataset".

PaigeMiller
Diamond | Level 26

@MikeFox wrote:

I think the documentation here is lacking. What is really needed is a definition of the word "levels": one might infere it but, since it's quite easy to come up with a robust one, it should be added into the official documentation. For example "A level of a variable is one of the unique values that such variable assumes in the dataset".


Earlier in this thread, someone discussed the meaning of "level" according to the English language. However that's irrelevant here, level has a meaning in statistical language that doesn't have to correspond to English dictionary meanings of the word, and it is exactly what you say.

--
Paige Miller
MikeFox
Fluorite | Level 6

I wasn't aware of this. My guess is that SAS being born as a tool for people with a strong background in statistics the definition of levels is assumed to be well known, and it would have been redundant to have it added into the documentation. However, since SAS has grown and expanded to reach people with a more diverse background I still think it wouldn't hurt to add a reminder. Thanks for the clarification.

ballardw
Super User

One use is to compare expected numbers of responses to a variable. See the following example:

ods select nlevels;
proc freq data=sashelp.class  nlevels;
ods output nlevels=work.classlevels;
run;

 

which creates in the results window

 

Number of Variable
Levels
Variable Levels
Name 19
Sex 2
Age 6
Height 17
Weight 15

 

So if "knew" I have 19 records and expect 19 unique names then I don't have to examine the actual values (somewhat useful for identifying the presence of duplicates when you have 1000's of records). If I had expected two levels of Sex and something like 8 I would suspect something wrong.

This rough information could also be used to select or reduce variables for examination with the full Proc Freq output (run without the ODS Select just preceding).

I often run Proc Freq on data sets as an initial data check. BUT if I have 1000's of records and may have some variables that seldom or don't duplicate such as name, phone number, account number and similar then I can reduce the proc freq output by examining the Nlevels and then run something like

Proc freq data=have(drop=name phone account);

run;

to get tables of the values other than those variables.

 

The output data set created with the ods output statement also has additional information about missing levels. Which if you don't expect any missing values for a variable and have NMissLevels greater than 0 you have at least one missing value. If you use the special missing values .A to .Z in addition to . then the NMissLevels variable will indicate how many have been used. If you don't think you were using special missing and see a value greater than 1 that tells you that assumption was incorrect.

drpartonslab
Calcite | Level 5

I had the same question. I guess I'm confused about the word "level" being used in this context. The English word "level" means a layer of a building (e.g. Ground level) or a rank in scale or size (e.g. at national/international level) or a tool that tests whether a surface is flat (i.e. a spirit level). Does the term "number of variable levels" depict rankings/layers of the variable of interest? Or does it just mean: a list of values of a variable?

PaigeMiller
Diamond | Level 26

As explained above, NLEVELS is the number of distinct values of a variable. In SASHELP.CLASS, NLEVELS=2 for variable SEX and NLEVELS=17 for variable HEIGHT.

--
Paige Miller
Ksharp
Super User

If you were familiar with SQL. it is the same thing as :

 

proc sql;
select count(distinct sex) as nlevels
 from sashelp.class;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 21609 views
  • 10 likes
  • 7 in conversation