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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

@RZB 

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 

RZB
Calcite | Level 5 RZB
Calcite | Level 5
thanks i still trying
Kurt_Bremser
Super User

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
RZB
Calcite | Level 5 RZB
Calcite | Level 5

thank you, 

 

  modifying the rutine with others delimiters and another variables, i'm near the final result i need.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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