BookmarkSubscribeRSS Feed
lathabrewer
Calcite | Level 5

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!!!

1 REPLY 1
Reeza
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 888 views
  • 0 likes
  • 2 in conversation