DATA Step, Macro, Functions and more

Export dataset by ID to mutiple pipe delimited text files

Reply
N/A
Posts: 1

Export dataset by ID to mutiple pipe delimited text files

Hi guys,

I am fairly new to SAS and have been trying to figure of how to export a dataset into multiple pipe delimited text files based on the field PPG ID.

Lets assume my input dataset named stars_report_in is like this

PPG IDMember IDMember NameCycle End Date
1011123David Thorne04/07/2015
1011234Macy Davis
04/07/2015
5674567Deanna Summers
04/07/2015
5674789Tabitha Adams
04/07/2015

I want text files created with file naming convention like this: <PPG ID>_Report_<Cycle End Date>.txt.

So first text file generated will be named 1011_Report_04072015.txt and will have data in pipe delimited format like this

1011|123|David Thorne|04/07/2015

1011|234|Macy Davis|04/07/2015

Second text file will be named 5674_Report_04072015.txt and will contain data in pipe delimited format like this below

5674|567|Deanna Summers|04/07/2015

5674|789|Tabitha Adams|04/07/2015

,pipe_delimited

and so on. Any help on this would be really appreciated.  I use SAS EG 7.1 to write all my code.

Thanks!!!

Super User
Posts: 17,963

Re: Export dataset by ID to mutiple pipe delimited text files

You'll need to explicitly write your export in a data _null_ step but then you can use the FILEVAR option to split the files automatically. 

 

Here's an example based on the SASHELP.CARS dataset.  Note the DLM option used to specify the delimiter and that the header names are explicitly defined. 

 

PROC SORT DATA=SASHELP.CARS OUT=CARS;
BY make;
RUN;


DATA _NULL_;

SET cars; *Dataset to be exported;
BY make; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.make then out_file=cats('C:\_localdata\temp\', trim(make), ".txt");

file temp filevar=out_file dlm='|' dsd;

*If first value of make then output column names;
if first.make then 
put 'Make|Model|MPG_HIGHWAY|MPG_CITY';

*Output variables;
put make model mpg_highway mpg_city;

run;
Ask a Question
Discussion stats
  • 1 reply
  • 196 views
  • 0 likes
  • 2 in conversation