BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
6 REPLIES 6
deleted_user
Not applicable
yes - you may use the _N_ variable which is the SAS counter I believe - increments by 1 each loop of the DATA step.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
DanielSantos
Barite | Level 11
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3721 views
  • 0 likes
  • 3 in conversation