DATA Step, Macro, Functions and more

Using a loop counter as part of a data step/variable name

Reply
N/A
Posts: 0

Using a loop counter as part of a data step/variable name

Is it possible to use a counter as part of a data step or another sas variable name?

For instance:"

do i = 1 to 100;
data month&i;
set January;
run;
end;

Is there anything to that effect?
N/A
Posts: 0

Re: Using a loop counter as part of a data step/variable name

yes - you may use the _N_ variable which is the SAS counter I believe - increments by 1 each loop of the DATA step.
Super Contributor
Super Contributor
Posts: 3,174

Re: Using a loop counter as part of a data step/variable name

You cannot mix SAS variables and SAS macro variables, as you have demonstrated. Also, the SAS variable _N_ identifies the current observation number, in most circumstances during DATA step processing.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 474

Re: Using a loop counter as part of a data step/variable name

This is not possible:

do i = 1 to 100;
data month&i;
set January;
run;
end;

but, this is and actually works:

%macro my_macro;

%do i = 1 %to 100;
data month&i;
set January;
run;
%end;

%men my_macro;
%my_macro;

datastep do...loop (the one without the %) is not permitted outside the datastep.

macro do...loop (the one with the %) is permitted inside/outside the datastep but within a macro definition.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
Super Contributor
Super Contributor
Posts: 3,174

Re: Using a loop counter as part of a data step/variable name

Suggest that the OP back up and reply to this post with detailed information about what is to be accomplished, for the most accurate input and thread discussion. Providing an example of both INPUT and desired OUTPUT would be most helpful.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Using a loop counter as part of a data step/variable name

Sure Scott,

I am creating a data set for each month of the year. Instead of creating 12 data sets in a list, I want to use a loop and name the data sets Month1 -
Month 12.

Inside the data set I want to use a macro variable array that has date parameters. For example, Month_ST(1) = '01Jan2008'd....This way I can test to see if something happened during the proper time period for a particular month.

I hope that makes things a bit clearer. Thanks for everyones help.
Super Contributor
Super Contributor
Posts: 3,174

Re: Using a loop counter as part of a data step/variable name

Okay - you want to create MONTH1 through MONTH12 SAS files - separate with each file only having the specific MONTH value which would be derived from a SAS DATE variable?

You can do this with SAS macro language as was mentioned in the previous post-reply. The macro code will however also need to generate the SAS DATA step IF/THEN/OUTPUT statements and the IF portion will need to use the MONTH function to test against some SAS DATE variable and use OUTPUT to the particular MONTHn-MONTHnn file.

Or, manually code the DATA step with your 12 output files, a SET statement for your single input file, and then handcode the IF/THEN/OUTPUT/ELSE.... to use the MONTH() function and direct the observation to the appropriate/desired output file.

The SAS support website http://support.sas.com/ provides SAS-hosted product documentation and also supplemental technical and conference topic papers - a few of which I found and have pasted below for reference.

Scott Barry
SBBWorks, Inc.

SASâ Macro Programming for Beginners
Susan J. Slaughter, Avocet Solutions, Davis, CA
Lora D. Delwiche, Delwiche Consulting, Winters, CA
http://www2.sas.com/proceedings/sugi29/243-29.pdf


SAS® Macro Programming Tips and Techniques
Kirk Paul Lafler, Software Intelligence Corporation, Spring Valley, California
http://support.sas.com/resources/papers/proceedings09/151-2009.pdf
Ask a Question
Discussion stats
  • 6 replies
  • 220 views
  • 0 likes
  • 3 in conversation