How to write records from one SAS dataset to multiple ASCII files or SAS datasets?

labradorchik used Ask the Experts™
Hi everyone,

in SAS I am trying to sort one SAS dataset by a certain variable (var1) and then write all records to SAS datasets (name includes "var1") and ASCII files (name includes "var1"). I am thinking to do everything in two steps as below but I am not sure if Step 2 should be done in PROC SQL or maybe in a DATA step with macros?  If anyone knows how to do Step 2 please let me know. Any suggestions or examples would be greatly appriciated.  

Step 1. Sort olddata.sas7bdat SAS dataset by var1
Step 2. Write all records from olddata.sas7bdat for each unique var1 to its correcponding data<var1>.sas7bdat SAS datasets and data<var1>.in ASCII files

* Step 1- sorting initial dataset
proc sort data=olddata;
   by var1;

* Step 2 - creating data<var1>.sas7bdat
proc sql noprint;
  select distinct var1 into : x separated by ' ' from olddata;
%macro create;
  %do i = 1 %to %eval(%sysfunc(count(&x, %str( )))+1);
  data data%scan(&x,&i.);
  set olddata;
  if var1 = "%scan(&x,&i.)";

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


I am not sure if my question is in correct categories. Please advise if it is not. Main category should probobly be "SAS" since the question is related to SAS language. Thanks in advance!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial