Last active
June 2, 2021 20:21
-
-
Save statgeek/047bc83a85672f4dd546 to your computer and use it in GitHub Desktop.
Split a data set into multiple text files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This program will spit a data set into multiple csv files each with a header row | |
and the data row that meets the condition of a group. If you want a record for each row use a unique identifier | |
as your var_split. | |
Note that this may not work if you have a very large data set that cannot be sorted or the number of variables are | |
over 150ish | |
*/ | |
*name of the data set with the original data; | |
%let lib_name = sashelp; | |
%let dsn_name = class; | |
*Variable to split on; | |
%let var_split = NAME; | |
*path to folder to save text files; | |
%let path_folder= /home/fkhurshed/; | |
*if you are exporting each line this is not required | |
but should not cause any issues unless your data set is large. In that case rename your data set to _temp and skip this step; | |
PROC SORT DATA=&lib_name..&dsn_name OUT=_temp; | |
BY &var_split; | |
RUN; | |
*make variable lists; | |
*for header row; | |
proc sql noprint; | |
select name into :var_list_csv separated by ", " from sashelp.vcolumn where libname = upper("&lib_name") and memname = upper("&dsn_name"); | |
select name into :var_list separated by " " from sashelp.vcolumn where libname = upper("&lib_name") and memname = upper("&dsn_name"); | |
quit; | |
DATA _NULL_; | |
SET _temp; *Dataset to be exported; | |
BY &var_split.; *Variable that file is to be split on; | |
*Create path to file that is to be exported; | |
if first.&var_split. then out_file=cats("&path_folder.", &var_split., ".csv"); | |
file temp filevar=out_file dlm=',' dsd; | |
*If first value of make then output column names; | |
if first.&var_split. then | |
put "&var_list_csv."; | |
*Output variables; | |
put &var_list.; | |
run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment