DATA Step, Macro, Functions and more

Scripting and Macro use

Reply
N/A
Posts: 0

Scripting and Macro use

I'm a STATA user trying to start working in SAS, and finding that I cannot figure out how to do some of the programming things I could do easily in Stata. I'm hoping someone can help.

For example, if I write the following code in Stata:

forvalues i=1/5 {
save d:\myfolder\datafile`i'.dta
}

the value i (called local macro in Stata) would increment from 1 to 5, and the `i' in the filename would be replaced with value characters 1 to 5. I would save 5 files with slightly different names.

Is there equivalent code in SAS? How do I run code like that outside the confines of a procedure or data step? In Stata, everything is interactive, so I can script just by typing a series of commands. In SAS, I cannot just start typing

DO i=1 to 5

Thanks

Paul
Frequent Contributor
Posts: 127

Re: Scripting and Macro use

Hello,

Could you please provide us with more info about the following technical aspects:

- Are the files being created based on one or many SAS datasets ?
- What kind of files do you need to create ? (CSV, EXCEL, ... etc.)
- What are the selection criteria to apply on your data before knowing in which file you'll write the data ?

Regards,
Florent
N/A
Posts: 0

Re: Scripting and Macro use

Thanks. You miss my point a little. I am asking how to use the macro variable in SAS, and get the program to run.

I am trying to save output from analysis following a simulation of data. There will be lots of replicates.

However, the question is more general than that. In Stata, I can just start typing code, and it is very simple. In SAS, it seems to complain unless code is done within the confines of a Procedure or Data step. I want to write code that surrounds and manages procedures and data steps, that won't be a part of either, and SAS feels really clunky in that regard. So again, without going into detail on the data, how would I save 5 different copies of the same data set with slightly different names using a macro to change the target file name?

Thanks

Paul
Frequent Contributor
Posts: 127

Re: Scripting and Macro use

By using macro code, it can be done that way:

%MACRO createFiles(number); /* MACRO definition */
%DO i= 1 %TO &number; /* Do loop */
data _NULL_; /* Creation of file */
file "d:\myfolder\datafile&i..dta";
run;
%END;
%MEND createFiles;

%createFiles(5); /* Number of loops is defined as a parameter of the macro */


Hope it helps.

Regards,
Florent
N/A
Posts: 0

Re: Scripting and Macro use

Thanks. It does help. It's like Stata, but I have to add those characters % and & in the right places to make it run.

P
Ask a Question
Discussion stats
  • 4 replies
  • 164 views
  • 0 likes
  • 2 in conversation