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