DATA Step, Macro, Functions and more

Data steps in do loops - possible?

Not applicable
Posts: 0

Data steps in do loops - possible?

I want to create 10 different data sets using the data step method. The only parameter that would affect the content of the data sets is the year, 1990-1999.

Is it possible to use a do loop to create the sets (inside a data step?) or is it necessary to write a macro?

I would prefer not using a macro.

But data steps inside data steps? And the naming of the different sets?

Super Contributor
Super Contributor
Posts: 3,176

Re: Data steps in do loops - possible?

Posted in reply to deleted_user
Yes you can, using a single DATA step, however you must list each of the 10 SAS data sets on the DATA statement for the SAS DATA step.

Explore SAS documentation and using the MDY function (within the DATA step) to assign a SAS DATE (numeric type) variable in the DO loop, incrementing the year-portion of your SAS variable via the MDY function. Apply a SAS FORMAT statement to your SAS variable.

Scott Barry
SBBWorks, Inc.
Posts: 9,368

Re: Data steps in do loops - possible?

Posted in reply to deleted_user
I'm not sure what you mean by using a do loop to create the data sets.
There are 2 ways to create data set with a DATA step program:
1) read "raw" data using INFILE/INPUT to create your output data set
2) read an existing SAS dataset using a SET statement (you can also read a DBMS file with the right SAS/Access product)

If you are reading data with a SET statement and you want to output to differently numbered datasets based on a value, such as YEAR,
you don't really need a do loop to accomplish this task:

data sale93 sale94 ;
set sashelp.prdsale;
if year = 1993 then output sale93;
else if year = 1994 then output sale94;

proc print data=sale93;
title '1993 Sales';

proc print data=sale94;
title '1994 Sales';

If, for example, the data had the possibility of 1990-1999, but some years were not in the data and you wanted to programmatically run a data step
program for each year, then the way to accomplish that might best be in a macro program.

It really depends on what you want to do.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation