BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
4 REPLIES 4
Florent
Quartz | Level 8
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
deleted_user
Not applicable
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
Florent
Quartz | Level 8
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
deleted_user
Not applicable
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 794 views
  • 0 likes
  • 2 in conversation