SAS Infile not reading any data rows - sas

I am a novice with SAS and am having trouble importing a CSV file. The information is in the following layout:
aircraft,duration,no_pasg,speed_ground,speed_air,height,pitch,distance
boeing,98.4790912,53,107.91568,109.3283765,27.41892425,4.043514571,3369.836364
boeing,125.7332973,69,101.6555886,102.8514051,27.80471618,4.117431699,2987.803924
boeing,112.0170008,61,71.05196088,,18.58938573,4.434043129,1144.922426
I run the code below and I get 0 observations (not expected) and 8 variables (expected). What am I doing wrong? I even tried putting the firstob=2 to skip the first row that contain headers but that hasn't helped
I appreciate the help.
DATA FAA_DATA1;
INFILE '~/Project/Data/FAA1.csv' dsd dlm=',' firstobs=2;
INPUT aircraft $ duration no_pasg speed_ground speed_air height pitch distance;
RUN;
PROC PRINT;
RUN;

Sounds like it is not seeing the end-of-line characters and so thinks your file is one long line. That would explain why FIRSTOBS=2 causes you to get 0 observations. There should be a note in the log about how many lines SAS read from the file.
Try using the TERMSTR= option on INFILE statement. The normal end of line for Unix is TERMSTR=LF. The normal end of line for Windows is TERMSTR=CRLF. If you made the file using Excel on a Mac then you should try using TERMSTR=CR.
For some reason Excel on a Mac still thinks that Mac OS uses CR as the end-of-line character for text files even though Apple converted the Mac OS to using Unix years ago. There also should be an option in Excel when saving the file to save it as comma delimited but using normal end of line characters.

Related

Data incorrectly read in as missing when using DSD option in infile statement (SAS)

I am trying to read the following data from a Notepad (text) file into a SAS data set:
name1,124325,08/10/2003,1250.03
name2,114565,08/11/2003,11115.11
name3,000007,08/11/2003,12500.02
When I use this SAS code:
data new;
filename tfile '~\transact2.txt';
infile tfile dsd;
input name $ id date mmddyy10. cost 8.2;
run;
I get this, where cost is all missing:
However, if I just replace dsd with dlm=',', then the cost variable is read in correctly. Why does dsd cause the cost variable to be read in incorrectly?
dsd does not say "use a delimiter". It tells SAS how to use that delimiter (mostly, saying anything in quotes is treated as one field, and modifying how consecutive delimiters are treated). dlm=',' is necessary to read this in correctly. I'm a bit surprised you got as close to correct as you did. (Fortunately, SAS makes some assumptions here that end up making it work correctly, more-or-less).
Also, you're mixing two styles of input, which isn't allowed.
When you use delimited input, you are using list, not column, input. You can only indicate character/not character, and cannot use informats directly. If you want to embed the informats like you do for the date, you need to use modified column input:
data new;
filename tfile '~\transact2.txt';
infile tfile dsd;
input name $ id date :mmddyy10. cost;
run;
Also note that reading in cost with 8.2 is incorrect. The decimal in an informat is only for reading in 12345678 as 123456.78 (back in the day when you had 80 column cards and didn't want to spend one on the decimal). In general in "modern" SAS you should not be using decimal portion of informat ever. SAS will see the decimal and work it out properly.

Export SAS dataset in Excel's "Text (MS-DOS)" format

I need to export a data set as text file for an ancient batch process probably running on Unix. The file has one column and all fields are numeric.
I want to create a text file which emulates the way Excel creates Text (MS-DOS) files:
Saves a workbook as a tab-delimited text file for use on the MS-DOS
operating system, and ensures that tab characters, line breaks, and
other characters are interpreted correctly. Saves only the active
sheet.
What is the best way to achieve this?
DOS uses encoding page 437, which is a very limited set of characters. If you don't have any special characters, you're good. If you do have special characters, you'll need to change the encoding page to 437 in order to guarantee character compatibility. This can be done as a dataset option.
SAS internally names this pcoem437. You can see the difference in output by changing the encoding= option.
data have;
input var$;
datalines;
ElNiƱo
ElNino
;
run;
proc export data=have(encoding=pcoem437)
file='C:\Directory\want.txt'
dbms=tab
replace;
run;
If you just have one column then the delimiter doesn't matter. You can write the file using a DATA step very easily.
data _null_;
set have ;
file 'myfile.txt' ;
put VAR1 ;
run;
If you want to add an extra line with the column name then add this before the PUT statement.
if _n_=1 then put 'VAR1';
If you are worried about whether you need to generate LF or CRLF for the end of line you can control that with the TERMSTR= option on the FILE statement.

Sas .dat file without column headings only 1st row being read in sas studio

I'm using Sas studio university edition. I have a dat file without any column headings (4 columns). I'm trying to read it in with
data van;
infile "/folders/myfolders/test2/psek-win.dat";
input a $ b $ c $ d $;
run;
i.e. create my own names for the columns. It works, but only the very first line is being read in. How can I get it to read all lines? I have been watching youtube tutorials but I'm stuck.
EDIT: Here is the output
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
57
58 data van;
59 infile "/folders/myfolders/test2/psek-win.dat";
60 input a $ b $ c $ d $;
61 run;
NOTE: The infile "/folders/myfolders/test2/psek-win.dat" is:
Filename=/folders/myfolders/test2/psek-win.dat,
Owner Name=sasdemo,Group Name=sas,
Access Permission=-rw-rw-r--,
Last Modified=05Jun2015:06:55:44,
File Size (bytes)=1527
NOTE: 1 record was read from the infile "/folders/myfolders/test2/psek-win.dat".
The minimum record length was 1527.
The maximum record length was 1527.
NOTE: The data set WORK.VAN has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
62
63 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
75
EDIT 2:
I really should have included this info from the start...sorry:
I'm using Windows 7, running Sas Studio through VirtualBox on Linux Redhat 64. I realized the file I was using was meant for Windows. When I use a Linux version of the same file it works fine.
Thanks
Most of the time, if SAS is only reading one line and then acting as if it were done when it has many lines, it is a problem with the line terminator (ie, the end-of-line character(s)).
Windows uses CR+LF (0D0A) while Unix/Linux use 0A (LF), including Mac OS X.
This means that if you're on a Windows machine and have a Unix terminated file, it will be read in as if it has only one line. The other way around usually isn't a problem - you get an extra character that might make a bit of a mess, but it still acts as if it were the right number of lines.
You can alter this in the datastep by specifying the termstr= option to specify the correct line terminator. So for example:
data van;
infile "/folders/myfolders/test2/psek-win.dat" termstr=lf;
input a $ b $ c $ d $;
run;
Would instruct SAS to consider a simple line feed to be a line terminator.
I realized the file I was using was meant for Windows. When I use a Linux version of the same file it works fine. Thanks for the help

How to import txt file in SAS using infile command

I 'm beginner in SAS and currently using SAS 9.1 I want to import txt file using infile command but it is giving error. My code is as follows
data sasdata.twenty;
infile "C:\Users\Ravi Raghava\Desktop\Cricket.txt" firstobs=2;
input Position Runs Sixes Fours balls;
run;
Any help will be highly appreciated.
Usually I do it by the following:
proc import datafile='data.txt'
out=TableName
replace;
delimiter='09'x;
run;
It works correctly.
you are using the default sas input (i.e. maybe space is used as a separator, guess whether I meant numeric or alpha-numeric and so on). You assume the SAS knows what it is doing. You know what happens when you assume.
Use a
Length Positions $20 Runs Sixes Fours balls 8;
statement. That will at least make sure the first word is treated as alpha numeric and the rest as numbers. also use the
infile ..... dlm=',';
if you got a CSV file. or
infile ..... dlm='09'x;
if it is a tab separated file.

Can SAS 9.1.3 read csv file with utf8 BOM?

I am using SAS 9.1.3 in AIX 5.3
I have to proc import a CSV file using SAS.
The first line of CSV are column names.
SAS reports error in the log.
Then, I find out that the CSV file has 3 characters
(which is the utf8 byte order mark).
at the very beginning of the file.
I tried to use :
filename XXX 'XXXXXXXXXX' BOM ;
But, this is syntax error.
I replace BOM with BOMFILE, still syntax error.
It seems that SAS 9.1.3 cannot recognize the BOM options.
Does anyone have similar experience ?
Instead of the import procedure, you might try a data step like the following:
data test;
infile "data.csv" firstobs=2 dlm=','; /* assuming delimiter is a comma */
input /* use Input with $UTF8Xw. informat */
field1 $utf8x3. /* input fields 1 through 3 */
field2 $utf8x10.
field3 $utf8x3.
;
run;
SAS can read this (at least 9.1 plus) but your SAS session must be running with the DBCS and encoding options set.
-DBCS
-encoding UTF-8
These need to be in the sasconfig file or on command line of invocation. With these options the default encoding is Unicode for the SAS session. Without it Unicode options pass syntax checks but have no effect.
You can try using the encoding= options infile statement but for me that never worked.
For some related info see also http://www.phuse.eu/download.aspx?type=cms&docID=3658