- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You are missing the macro keyword:
%macro _count (namedata);
data &namedata;
set &namedata;
count=_N_;
run;
%mend;
%_count(data1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
yes, but it's more than that
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I don't see an easier way to move the observation counter to a variable. Way do you need the counter?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That seems like a very non-standard way to store your data.
Can you explain more about what you're trying to do overall?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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