SAS Enterprise Guide

Desktop productivity for business analysts and programmers
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;



sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2885 views
  • 1 like
  • 4 in conversation