Hello,
I have a problem splitting a data set into multiple txt with title variables and data splitted by differents combinations of variables.
I have this code;
DATA _NULL_;
SET final_7;
call symput('HH',trim(left(put(H,Z2.))));
call symput('MI',trim(left(put(M,Z2.))));
call symput('SS',trim(left(put(S,Z2.))));
CALL SYMPUT("FECHAOUT",TRIM(LEFT(put(FECHA,YYMMDD6.))));
CALL SYMPUT("CCC",CAD);
FILE "&OUTPATH.\HK&fechaout.&HH.&MI.&SS.&CCC..TXT";
HFIN=HORNOT+DURANOT;
B='09'X;
IF _N_=1 THEN PUT @1 CADENA Z4. B PROG B HORNOT TIME. B HFIN TIME. B NOTICIA;
RUN;
DATA _NULL_;
SET final_7;
call symput('HH',trim(left(put(H,Z2.))));
call symput('MI',trim(left(put(M,Z2.))));
call symput('SS',trim(left(put(S,Z2.))));
CALL SYMPUT("FECHAOUT",TRIM(LEFT(put(FECHA,YYMMDD6.))));
CALL SYMPUT("CCC",CAD);
FILE "&OUTPATH.\HK&fechaout.&HH.&MI.&SS.&CCC..TXT" MOD;
B='09'X;
PUT @1 PATROC B HORPRE TIME. B DURPRE TIME. B MENCIO;
RUN;
this code writes an HK&fechaout.&HH.&MI.&SS.&CCC..TXT with the variable titles and another one with all data set without titles and without splitting.
I need differents txt files including in each file the variable names:
CADENA ,PROG , HORNOT , HFIN ,NOTICIA;
and the data splitted by
FECHAOUT , HH, MM, SS ,CCC
with the data of these variables:
PATROC HORPRE DURPRE MENCIO;
The txt files my be like this :
HK1908191630090006.txt where 190919 is fechaout, 16 is HH, 30 is MM,09 is SS,and 0006 is CCC.
and data into file may be :
0001 Program 16:30:09 17:27:58 football
Citroën 16:55:35 0:00:04 S
Citroën 16:55:50 0:00:03 S
Citroën 16:57:26 0:00:05 S
Salud 17:25:11 0:00:03 S
Salud 17:25:18 0:00:03 S
where 0001 is CADENA, Program is PROG ,16:30:09 is HORNOT, 17:27:58 is HFIN and football is NOTICIA
and the data from data set citroen is PATROC, 16:55:35 is HORPRE, 0:00:04 is DURPRE and football is MENCIO
many thanks!
See this example using sashelp.class as a source and the column sex as a group variable:
proc sort data=sashelp.class out=class;
by sex name;
run;
data _null_;
set class;
by sex;
fname = cats('$HOME/sascommunity/class_',sex,'.txt');
file dummy filevar=fname dlm=',' dsd;
if first.sex then put 'name,age,height,weight';
put name age height weight;
run;
And these are the resulting files:
class_F.txt:
name,age,height,weight Alice,13,56.5,84 Barbara,13,65.3,98 Carol,14,62.8,102.5 Jane,12,59.8,84.5 Janet,15,62.5,112.5 Joyce,11,51.3,50.5 Judy,14,64.3,90 Louise,12,56.3,77 Mary,15,66.5,112
class_M.txt:
name,age,height,weight Alfred,14,69,112.5 Henry,14,63.5,102.5 James,12,57.3,83 Jeffrey,13,62.5,84 John,12,59,99.5 Philip,16,72,150 Robert,12,64.8,128 Ronald,15,67,133 Thomas,11,57.5,85 William,15,66.5,112
There is too much detail in your question for me to try and provide code.
I believe you're looking for FILEVAR. This SAS Note is eventually all you need: http://support.sas.com/kb/24/599.html
See this example using sashelp.class as a source and the column sex as a group variable:
proc sort data=sashelp.class out=class;
by sex name;
run;
data _null_;
set class;
by sex;
fname = cats('$HOME/sascommunity/class_',sex,'.txt');
file dummy filevar=fname dlm=',' dsd;
if first.sex then put 'name,age,height,weight';
put name age height weight;
run;
And these are the resulting files:
class_F.txt:
name,age,height,weight Alice,13,56.5,84 Barbara,13,65.3,98 Carol,14,62.8,102.5 Jane,12,59.8,84.5 Janet,15,62.5,112.5 Joyce,11,51.3,50.5 Judy,14,64.3,90 Louise,12,56.3,77 Mary,15,66.5,112
class_M.txt:
name,age,height,weight Alfred,14,69,112.5 Henry,14,63.5,102.5 James,12,57.3,83 Jeffrey,13,62.5,84 John,12,59,99.5 Philip,16,72,150 Robert,12,64.8,128 Ronald,15,67,133 Thomas,11,57.5,85 William,15,66.5,112
thank you,
modifying the rutine with others delimiters and another variables, i'm near the final result i need.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.