Looping through observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Looping through observations

Hi all

I have a macro which accepts a dataset name as parameter. I need to run through the observations one by one, putting each observation through another macro to fit non-linear curves, amongst other things. My code looks something like this:

%macro fitCurves(inputData, outPutData);

..........................................

%do i=1 %to 286;

     ...........................

  %fitCurve(work.Line_temp,work.fittedVals_temp);


     ....................

%end;

%mend fitCurves;


My question is about the number of lines in the input dataset, which I use in the for loop. How can I do it so that the number of observations isn't hardcoded, so that the macro can handle databases of different sizes?


Thanks in advance.


Accepted Solutions
Solution
‎08-13-2013 08:04 AM
Super Contributor
Posts: 543

Re: Looping through observations

Hi.

One way would be to use something like this:

proc sql;

  select count(distinct name) into: my_cnt

  from sashelp.class;

quit;

%put &my_cnt.;

Where name is your variable of interest you want to loop through...

then you can use...

%macro fitCurves(inputData, outPutData);

..........................................

%do i=1 %to &my_cnt.;

     ...........................

  %fitCurve(work.Line_temp,work.fittedVals_temp);


     ....................

%end;

%mend fitCurves;


Good luck,

Anca.

View solution in original post


All Replies
Solution
‎08-13-2013 08:04 AM
Super Contributor
Posts: 543

Re: Looping through observations

Hi.

One way would be to use something like this:

proc sql;

  select count(distinct name) into: my_cnt

  from sashelp.class;

quit;

%put &my_cnt.;

Where name is your variable of interest you want to loop through...

then you can use...

%macro fitCurves(inputData, outPutData);

..........................................

%do i=1 %to &my_cnt.;

     ...........................

  %fitCurve(work.Line_temp,work.fittedVals_temp);


     ....................

%end;

%mend fitCurves;


Good luck,

Anca.

Super Contributor
Posts: 333

Re: Looping through observations

You should be able to add the following code to get the total number of observations into a macro variable &nobs:

data _null_;

  set &ds nobs=tobs;

  call symput('nobs',tobs);

run;

NOTE in your code replace 286 with &nobs;

Like anything else in SAS there are about 100 ways to do it.

EJ

Super Contributor
Posts: 339

Re: Looping through observations

Can you provide the way you're building your work.line_temp? The approach I am thinking of is using

%let dsid = %sysfunc(open(&inputData));

do while %sysfunc(fetch(&dsid)) = 0;
...

end;

But I am not sure it is fit with how you do your whole macro processing and there might be a very easy solution to set nearly everything in a single data step using call execute for %fitCurve. Use the data step looping on your entire &inputData dataset, use call execute("%fitCurve(&inputData.(point=_N_), work.fittedVals_temp)");

or something similar to point= data set option to emulate whatever approach you've used to build work.Line_temp.

Anyway long story short, could you provide additionnal details on your macro for more detailed help than pointers of what tools exist?

Vince

Occasional Contributor
Posts: 6

Re: Looping through observations

Thanks Anca, that works perfectly.

Interesting that it needs a period after the macro variable name. Do you know why that is?

Chris

Super Contributor
Posts: 543

Re: Looping through observations

Hi Chris.

For the example I provided the macro variable does not need a period, however, I always use a period when I invoke a macro variable.

The one time you do need a period is when you have a macro variable followed by some text.

For example,

%let my_macro = something;

%put &my_macrovalue;

vs.

%put &my_macro.value;

(this is a really dumb example, sorry).

Smiley Happy

Glad it worked!

Trusted Advisor
Posts: 1,615

Re: Looping through observations

Actually, there is no requirement to put a period after the macro variable name. Everything will work fine without the period.

Some people automatically put the period after all macro variable names, while others put the period there only when necessary. There is a debate over which is a better programming method. Both work.

Super Contributor
Posts: 339

Re: Looping through observations

It doesn't always need to be there but it happens to be a delimiter for macro variables. For instance, if your macro variable holds a prefix of another macro variable that holds the value like

%let month=jan;

%let jan_2009=500;

%put &&&month_2009;

will attempt to resolve &month_2009 on first pass which does not exist or may represent a different value whereas

%put &&&month._2009.;

will properly resolve && to & and &month. to jan on first pass, and then resolve the remaining &jan_2009 on second pass to 500.

They are not always necessary. In your particular case bolded case they should not be essential for the macro processor to act appropriately.

Vince

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 561 views
  • 0 likes
  • 5 in conversation