BookmarkSubscribeRSS Feed
Bison370
Calcite | Level 5

Hi -

 

I am a long time SAS user but there are still some things I don't understand. When using nobs to count observations, as in 

 

data _NULL_;
 if 0 then set sashelp.cars nobs=n;
put "no. of observations =" n;
 stop;
run;

 

why is the syntax "nobs = n" instead of "n = nobs"? Any other time we want to create a new variable, we put the name of the variable on the left of the equal sign and the value on the right. Here it's the opposite. Can someone explain this to me?

3 REPLIES 3
PaigeMiller
Diamond | Level 26

The option in the SET command is NOBS=variablename. This is consistent with the way other options in the SET command work.

 

If you type 

 

if 0 then set sashelp.cars n = nobs;

 

you get an error, as there is no option in the SET command that is N=

--
Paige Miller
Tom
Super User Tom
Super User

You are confusing two totally different things. Both of which are common things to do in SAS.

In this statement:

set sashelp.cars nobs=n;

NOBS is the name of an option that the SET statement supports.  The equal sign separates the option name from the value being given for the option.  This particular option wants the NAME of a variable.  It will then populate that variable with the number of observations in the dataset.

These statements:

nobs=n;
n=nobs;
z=y+x;
index=1;

Are assignment statements.  The are used in data steps (and some PROCs that support coding).

The name of the variable that is being assigned the value comes first.  The equal sign indicates that is an assignment statement.  And everything after the equal sign is the expression that resolves to the value to be assigned.

 

Bison370
Calcite | Level 5

Thank you! This makes sense. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 850 views
  • 5 likes
  • 3 in conversation