The script presents 7 use cases for importing text data: standard CSV reading with the DSD option, using the tilde (~) modifier for advanced quote handling, explicit length definition, using multiple delimiters simultaneously, defining the delimiter via a variable, dynamically reading the delimiter from the data itself, and using a complete character string as a separator (DLMSTR).
Data Analysis
Type : CREATION_INTERNE
Data is included directly in the code via DATALINES and CARDS statements.
1 Code Block
DATA STEP Data
Explanation : Reading a standard CSV format. The `dlm=','` option specifies the comma as a separator and `dsd` (Delimiter Sensitive Data) correctly handles quotes and missing values.
Copied!
title '1.3.3a Delimited List Input Modifiers';
data base;
length lname $15;
infile datalines dlm=',' dsd;
input fname $ lname $ dob :mmddyy10.;
datalines;
'Sam','Johnson',12/15/1945
'Susan','Mc Callister',10/10/1983
run;
1
title '1.3.3a Delimited List Input Modifiers';
2
DATA base;
3
LENGTH lname $15;
4
INFILEDATALINES dlm=',' dsd;
5
INPUT fname $ lname $ dob :mmddyy10.;
6
DATALINES;
7
'Sam','Johnson',12/15/1945
8
'Susan','Mc Callister',10/10/1983
9
RUN;
2 Code Block
DATA STEP Data
Explanation : Using the `~` (tilde) modifier in the INPUT statement. Here it allows reading fields potentially containing delimiters (like the comma in 'Fresno, CA') while respecting the DSD structure.
Copied!
title2 'Using the ~ Format Modifier';
data base;
length lname $15;
infile datalines dlm=',' dsd;
input fname $ lname $ birthloc $~15. dob :mmddyy10. ;
datalines;
'Sam','Johnson', 'Fresno, CA','12/15/1945'
'Susan','Mc Callister','Seattle, WA',10/10/1983
run;
1
title2 'Using the ~ Format Modifier';
2
DATA base;
3
LENGTH lname $15;
4
INFILEDATALINES dlm=',' dsd;
5
INPUT fname $ lname $ birthloc $~15. dob :mmddyy10. ;
6
DATALINES;
7
'Sam','Johnson', 'Fresno, CA','12/15/1945'
8
'Susan','Mc Callister','Seattle, WA',10/10/1983
9
RUN;
3 Code Block
DATA STEP Data
Explanation : Reading where the length of variables is predefined by the `LENGTH` statement, allowing precise control over storage without a modifier in the INPUT.
Copied!
title '1.3.3c Delimited List Input Modifiers';
/* ... */
data base;
length lname birthloc $15;
infile datalines dlm=',' dsd;
input fname $ lname $ birthloc $ dob :mmddyy10. ;
datalines;
/* Données ... */
run;
1
title '1.3.3c Delimited List Input Modifiers';
2
/* ... */
3
DATA base;
4
LENGTH lname birthloc $15;
5
INFILEDATALINES dlm=',' dsd;
6
INPUT fname $ lname $ birthloc $ dob :mmddyy10. ;
7
DATALINES;
8
/* Données ... */
9
RUN;
4 Code Block
DATA STEP Data
Explanation : Defining multiple possible delimiters for the same file. Here `dlm='/,'` indicates that either the slash OR the comma can serve as a separator.
Copied!
title '1.3.3d Read delimited code with multiple delimiters';
data imports;
infile cards dlm='/,';
input id importcode $ value;
cards;
14,1,13
25/Q9,15
6,D/20
run;
1
title '1.3.3d Read delimited code with multiple delimiters';
2
DATA imports;
3
INFILECARDS dlm='/,';
4
INPUT id importcode $ value;
5
CARDS;
6
14,1,13
7
25/Q9,15
8
6,D/20
9
RUN;
5 Code Block
DATA STEP Data
Explanation : Using a variable (`dlmvar`) to pass delimiters to the `dlm=` option of the INFILE statement, offering more flexibility (e.g., dynamic parameterization).
Copied!
title '1.3.3e Read delimited code with multiple delimiters';
title2 'Using a variable to specify the delimiter';
data imports;
retain dlmvar '/,';
infile cards dlm=dlmvar;
input id importcode $ value;
cards;
/* Données */
run;
1
title '1.3.3e Read delimited code with multiple delimiters';
2
title2 'Using a variable to specify the delimiter';
3
DATA imports;
4
retain dlmvar '/,';
5
INFILECARDS dlm=dlmvar;
6
INPUT id importcode $ value;
7
CARDS;
8
/* Données */
9
RUN;
6 Code Block
DATA STEP Data
Explanation : Advanced technique: reading the first character of the line (`input dlmvar $1. @`) to determine the specific delimiter for that line, then re-reading the line (`infile cards dlm=dlmvar`) with this delimiter. (Note: The original source code contained a ' @code_sas_json...' artifact which has been cleaned here).
Copied!
title '1.3.3f Read delimited code with multiple delimiters';
title2 'Reading the delimiter during execution';
data imports;
infile cards;
input dlmvar $1. @;
infile cards dlm=dlmvar;
input id importcode $ value;
cards;
,14,1,13
/25/Q9/15
~6~D~20
run;
1
title '1.3.3f Read delimited code with multiple delimiters';
2
title2 'Reading the delimiter during execution';
3
DATA imports;
4
INFILECARDS;
5
INPUT dlmvar $1. @;
6
INFILECARDS dlm=dlmvar;
7
INPUT id importcode $ value;
8
CARDS;
9
,14,1,13
10
/25/Q9/15
11
~6~D~20
12
RUN;
7 Code Block
DATA STEP Data
Explanation : Using the `DLMSTR` (Delimiter String) option available in recent versions of SAS, allowing a complete character string (here ',,/') to be defined as a unique separator, rather than a list of individual characters.
Copied!
title '1.3.3g Use a delimiter string';
data imports;
infile cards dlmstr=',,/';
input id importcode $ value;
cards;
14,,/1/,,/13
/* ... */
run;
1
title '1.3.3g Use a delimiter string';
2
DATA imports;
3
INFILECARDS dlmstr=',,/';
4
INPUT id importcode $ value;
5
CARDS;
6
14,,/1/,,/13
7
/* ... */
8
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.