SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bogdan
Calcite | Level 5

Question 1

If I write

data data1;

set data1;

count=_N_;

run;

then it works, but how do I do that with a macro?

I wrote

%_count (namedata);

data &namedata;

set &namedata;

count=_N_;

run;

%mend;

%_count(data1);

but it doesn't have effect  (that is it does not create a count variable). I also added a "%let" or "%put" before "count" to no effect.

Question 2

I do that because I want to have a variable for the number of each observation. Is there a more direct way to extract the number of observation from dataset without creating a count variable ? I tried _N_, nobs, obs to no effect.

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

SASKiwi has the answer to Q1.

Q2: It would be great if the observation number were available as an automaic variable throughout SAS, but it isn't. It's available within the data step with _n_ as you already know, and i SQL using the montonic() function. So if you need to do reports based on observation no, you need to store it explicitly.

Data never sleeps

View solution in original post

9 REPLIES 9
SASKiwi
PROC Star

You are missing the macro keyword:

%macro _count (namedata);

data &namedata;

set &namedata;

count=_N_;

run;

%mend;

%_count(data1);

Bogdan
Calcite | Level 5

yes, but it's more than that

andreas_lds
Jade | Level 19

I don't see an easier way to move the observation counter to a variable. Way do you need the counter?

Bogdan
Calcite | Level 5

I need it in general, but in the particular case today was in order to plot a variable and I needed the x axis values (which is the observation number)

LinusH
Tourmaline | Level 20

SASKiwi has the answer to Q1.

Q2: It would be great if the observation number were available as an automaic variable throughout SAS, but it isn't. It's available within the data step with _n_ as you already know, and i SQL using the montonic() function. So if you need to do reports based on observation no, you need to store it explicitly.

Data never sleeps
Peter_C
Rhodochrosite | Level 12

there is a little-used part of SAS that provides the row-number

Have a look at the function CUROBS()

In SAS/AF it needs a dataset-id number parameter but needs no parameter in SAS/FSP (presumeably because the facilities in FSP address just one table at a time)

Bogdan
Calcite | Level 5

I had the word "macro" only forgot to write it when the question was posted. I tried again and this time it worked. I don't why it didn't work first time.

Reeza
Super User

That seems like a very non-standard way to store your data.

Can you explain more about what you're trying to do overall?

Ron_MacroMaven
Lapis Lazuli | Level 10

There is a macro function named Nobs that uses SCL code to open and close the data set

and return the Nobs(data).

usage:

%Let Nobs = %nobs(data=sashelp.class);

%put note nobs:&Nobs;

http://www.sascommunity.org/wiki/Macro_Nobs_function

Ron Fehd  macro fun. maven

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2170 views
  • 6 likes
  • 7 in conversation