BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
AZFXL
Calcite | Level 5

Dear SAS community,

 

I have a large csv file with 100+ columns to be split into multiple smaller csv files by group key column in the csv files. On top of that, I would also like to further sum up the respective variables by group key. Simple illustration is as below

 

data

groupvar1var2var3… var100
group1abc11… 1
group1def22… 2
group1ghi33… 3
group2jkl44… 4
group2mno55… 5
group2pqr66… 6
group3stu77… 7
group3vwx88… 8
group3yza99… 9

 

output

1. group1.csv

groupvar2var3… var100
group166… 6

2. group2.csv

groupvar2var3… var100
group21515… 15

3. group3.csv

groupvar2var3… var100
group32424… 24

 

Is there any possible way to do this?

Thank you very much in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%let csv= c:\temp\have.csv ; *the CSV file you have;
%let var=bp_status  ;  *the group variable for splitting;
%let out= c:\temp\  ;  *the path where you want to keep splitted csv file;



libname out v9 "&out.";
proc import datafile="&csv." out=have dbms=csv replace;
run;
proc freq data=have noprint;
table &var./out=level;
run;
data _null_;
 set level ;
 call execute(catt("data temp;set have;if &var.='",&var.,"';run;
 proc export data=temp outfile='&out.\",&var.,".csv' dbms=csv replace;run;"));
run;



View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

First, summarize:

proc summary data=have;
by group;
var var:;
output out=want sum()=;
run;

then create the csv files:

data _null_;
set want;
length fvar $200;
fvar = "path to the files/" !! strip(group) !! ".csv";
file dummy filevar=fvar dlm="," dsd lrecl=32767;
put "group,var1,....var100"; /* expand this statement for all variables */
put group var1-var100;
run;
PeterClemmensen
Tourmaline | Level 20

Do you really want a series of CSV files with 1 (2 with header) rows in each? 

 

Why?

Ksharp
Super User
%let csv= c:\temp\have.csv ; *the CSV file you have;
%let var=bp_status  ;  *the group variable for splitting;
%let out= c:\temp\  ;  *the path where you want to keep splitted csv file;



libname out v9 "&out.";
proc import datafile="&csv." out=have dbms=csv replace;
run;
proc freq data=have noprint;
table &var./out=level;
run;
data _null_;
 set level ;
 call execute(catt("data temp;set have;if &var.='",&var.,"';run;
 proc export data=temp outfile='&out.\",&var.,".csv' dbms=csv replace;run;"));
run;



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 1629 views
  • 1 like
  • 4 in conversation