Subscribe Bookmark RSS Feed

Read one record from file?

Highlighted
pmroz

Super User

Joined:

Jun 23, 2011

Hello JMP JSL people,

Is there a way to read just the first line of a text file? I've developed a system that reads delimited columns from a .CSV file. Now the users want new fields added to the input files, and some fields are renamed. The system has to be able to handle files in the new format as well as the old one, and do it transparently. If I could read the first line that would be great, as that line contains the field names.

Thanks,
Peter
3 REPLIES
mpb

Super User

Joined:

Jun 23, 2011

Example follows. In the case of CSV data replace the End of Field Tab with Comma. I got this by using the interactive text import preview on the specified file and copied the Source script embedded in the resulting JMP file. I removed the specific reference to the number of columns to allow the script to work with any number of columns.

Open(
"C:\Program Files (x86)\SAS\JMP\9\Support Files English\Sample Import Data\Animals_L.txt",
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( 1 ),
Year Rule( "10-90" )
)
)
pmroz

Super User

Joined:

Jun 23, 2011

I didn't see that "lines to read" parameter. Unfortunately that doesn't work for me. My file uses "|" as the delimiter. I get all data crammed into one column.

Here's my code:

surv_dataset = Open(
surv_file,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Other( "|" ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Scan Whole File( 1 ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( 1 ),
Year Rule( "10-90" )
)
);

But I notice if I include the columns statement it works correctly, even if the column names are wrong. So I'll go that route.

Thanks for your help.
mpb

Super User

Joined:

Jun 23, 2011

I ran the following code against this text file and it seemed to work:

First|Second|Third|Fourth
1|2|3|4
11|22|33|44

Open(
my_file,
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Other( "|" ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( 1 ),
Year Rule( "10-90" )
)
)