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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9 replies
  • 1394 views
  • 6 likes
  • 7 in conversation