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