2
votes

So I have created a macro, which works perfectly fine. Within the macro, I set where the observation will begin reading, and then how many observations it will read.

But, in my proc print call, I am not able to simply do:

(firstobs=&start obs=&obs)

Because although firstobs will correctly start where I want it, obs does not cooperate, as it must be a higher number than firstobs. For example,

%testmacro(start=5, obs=3)

Does not work, because it is reading in the first 3 observations, but trying to start at observation 5. What I want the macro to do is, start at observation 5, and then read the next 3. So what I did is this:

(firstobs=&start obs=%eval((&obs-1)+&start))

This works perfectly fine when I use it. But I am just wondering if there is a simpler way to do this, rather than having to use the whole %eval... call. Is there one simple call, something like numberofobservations=...?

2

2 Answers

3
votes

I don't think there is. You can only simplify your macro a little, within the %eval(). .

%let start=5;
%let obs=3;

data want;
set sashelp.class (firstobs=&start obs=%eval(&obs-1+&start));
run;

Data set options are listed here: http://support.sas.com/documentation/cdl/en/ledsoptsref/68025/HTML/default/viewer.htm#p0h5nwbig8mobbn1u0dwtdo0c0a0.htm

You could count the obs inside the data step using a counter and only outputting the records desired, but that won't work on something like proc print and isn't efficient for larger data steps.

You could try the point= option, but I'm not familiar with that method, and again I don't think it will work with proc print.

1
votes

As @Reeza said - there is not a dataset option that will do what you are looking for. You need to calculate the ending observation unfortunately, and %eval() is about as good a way to do it as any.


On a side-note, I would recommend making your macro parameter more flexible. Rather than this:

%testmacro(start=5, obs=3)

Change it to take a single parameter which will be the list of data-set options to apply:

%macro testmacro(iDsOptions);
  data want;
    set sashelp.class (&iDsOptions);
  run;
%mend;

%testmacro(firstobs=3 obs=7);

This provides more flexibility if you need to add in additional options later, which means fewer future code changes, and it's simpler to call the macro. You also defer figuring out the observation counts in this case to the calling program which is a good thing.