DATA Step, Macro, Functions and more

Small macro for counter

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Small macro for counter

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.


Accepted Solutions
Solution
‎04-18-2013 04:31 AM
Super User
Posts: 5,255

Re: Small macro for counter

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


All Replies
Super User
Posts: 3,102

Re: Small macro for counter

You are missing the macro keyword:

%macro _count (namedata);

data &namedata;

set &namedata;

count=_N_;

run;

%mend;

%_count(data1);

Contributor
Posts: 20

Re: Small macro for counter

yes, but it's more than that

Super Contributor
Posts: 259

Re: Small macro for counter

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

Contributor
Posts: 20

Re: Small macro for counter

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)

Solution
‎04-18-2013 04:31 AM
Super User
Posts: 5,255

Re: Small macro for counter

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
Valued Guide
Posts: 2,174

Re: Small macro for counter

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)

Contributor
Posts: 20

Re: Small macro for counter

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.

Super User
Posts: 17,784

Re: Small macro for counter

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

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

Regular Contributor
Posts: 198

Re: Small macro for counter

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

☑ This topic is SOLVED.

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

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