Dear SAS community
I have a csv dataset (a flat sheet) that contains 100 fields of data from 75 patients. Each patient only has 1 line of data and each patient is identified by the variable "pt_id". I need to write a code to export each line from each patient into a separate csv file, thus generating 75 csv files.
do you have an example of such a code?
thanks
Here's an example of splitting by group, in your case PT_ID would be your group.
https://gist.github.com/statgeek/047bc83a85672f4dd546The only issue with this code is it explicitly lists the variable names and you probably want to do that in a more automated fashion.....
EDIT: I updated my example in the link to be more generic, the file names will be the PT_ID as well in this example. HTH.
Hi Reeza
Much appreciated. what if the var_split is really from combining 2 variables. To create the unique pt_id I combined “subject_id” with “visit” by pt_id=subject_id||visit.
The code generated the single line dataset just fine, but it added the pt_id to the end of the dataset. I need to get rid of this variable “pt_id” from the exported datasheets. Alternatively can the code you provided me, handled 2 variables as the var_split?
Thanks
*name of the data set with the original data;
%let lib_name = sashelp;
%let dsn_name = class;
*Variable to split on;
%let var_split1 = NAME;
%let var_split2 = SEX;
*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_split1 &var_split2;
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_split1 &var_split2.; *Variable that file is to be split on;
*Create path to file that is to be exported;
if first.&var_split2. 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_split2. then
put "&var_list_csv.";
*Output variables;
put &var_list.;
run;
Untested, but this should work for two variables.
Dear Reeza
I ran it but encounter this error (please see attached log)
Many thanks for your help. Please let me know your thoughts
Thanks again
*name of the data set with the original data;
%let lib_name = sashelp;
%let dsn_name = class;
*Variable to split on;
%let var_split1 = NAME;
%let var_split2 = SEX;
*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_split1 &var_split2;
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_split1 &var_split2.; *Variable that file is to be split on;
*Create path to file that is to be exported;
if first.&var_split2. then out_file=cats("&path_folder.", &var_split1., "_", &var_split2., ".csv");
file temp filevar=out_file dlm=',' dsd;
*If first value of make then output column names;
if first.&var_split2. then
put "&var_list_csv.";
*Output variables;
put &var_list.;
run;
I've changed the line to as follows - this is the part that controls the file name.
if first.&var_split2. then out_file=cats("&path_folder.", trim(&var_split1.), "_", trim(&var_split2.), ".csv");
So now it will include the firstSplitVariable_secondSplitVariable in the name. If you'd like only one portion, remove it from the CATS() function.
Note that the IF FIRST. is not needed when generating the filename. The FILE statement will take care of noticing when the value of the FILEVAR= variable changes and switch files automatically.
The SORT is not needed either especially if you are only writing one line per file. But if you are writing multiple lines to the same file then processing the records in filename order it will saving some time as it will prevent SAS from opening and closing the same file multiple times.
this works! thanks so much !!
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.
Ready to level-up your skills? Choose your own adventure.