Desktop productivity for business analysts and programmers

Handling '.' as a delimiter

Reply
N/A
Posts: 0

Handling '.' as a delimiter

I thought that two below codes should work the same. However the second data step generates 0.175 and 0.1101 for the Grade variable while I assumed it had to generate 17.50 and 11.01 as the first data step does. Any idea why?

data StudentsGrades;
infile datalines;
input StudentName $
CourseName $
DateOfExam Date7.
Grade 5.2
Result $;
datalines;
George Math 19Dec05 1750 P
Susan Geography 01Feb05 1101 F
;
run;
data StudentsGradesDot1;
infile datalines dlm='.';
input StudentName $
CourseName $
DateOfExam Date7.
Grade 5.2
Result $;
datalines;
George.Math.19Dec05.1750.P
Susan.Geography.01Feb05.1101.F
;
run;

The results for the first data step:
George Math 16789 17.5 P
Susan Geograph 16468 11.01 F

The results for the second data step:
George Math 16789 0.175 P
Susan Geograph 16468 0.1101 F

Thanks,
Ramin
Esteemed Advisor
Posts: 5,198

Re: Handling '.' as a delimiter

For starters, having delimiters that can show up within the data often causes problems. I should try to get your file supplier to have the file delimited with something else.

If this is not feasible consider to read the whole record into a single string and then using different SAS character functions to read out your different columns.

Regards,
Linus
Data never sleeps
Super Contributor
Super Contributor
Posts: 3,174

Re: Handling '.' as a delimiter

This is yet another anomaly with the "date" related INFORMAT handling. The fact that there is mixed INPUT processing is a contributing factor. My recommendation is to open a SAS support track, to let SAS Institute know that this unexpected input handling occurs when there is a date input field, and where the delimiter DOES NOT occur in the input record.

For illustration purposes, the two different input record types are processed, one containing a date field within the record and a second type without a date. SAS seems to handle the record not containing the date with no problem. See the code below:

data _null_;
infile datalines dlm='\';
format numdtvar1 date9.;
input type $ @;
if type ne ' ' then do;
if type = '1' then input charvar1 $ numdtvar1 anydtdte. numvar1 4.2 charvar2 $;
else
if type = '2' then input charvar1 $ numvar1 4.2 charvar2 $;
putlog _all_;
end;
datalines;
1\aaaa\01jan2008\2222\bbbb
2\aaaa\2222\bbbb
run;

Like I suggested, open a support track and stand your ground. Good luck.


Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 105

Re: Handling '.' as a delimiter

You can use the informat statment

If i take your example its looks like this:

data b;
infile datalines dlm='.';
input StudentName $
CourseName $
DateOfExam Date7.
Grade
Result $;
informat Grade 5.2;
datalines;
George.Math.19Dec05.1750.P
Susan.Geography.01Feb05.1101.F
;
run;
Occasional Contributor BPD
Occasional Contributor
Posts: 12

Re: Handling '.' as a delimiter

To: RaminR

Use the informat modifier ':'.

This will read the variable content up to the next delimeter and then move the pointer to the next position after the delimeter. For some reason in your code SAS appears to be reading the '.' delimeter that separates values as part of the grade value.

In any event the following code works to read in the grade correctly. Be aware that you need to set your informats wide enough to accomodate the largest possible input to be read.

data StudentsGradesDot1;
infile datalines dlm='.';
input StudentName :$8.
CourseName :$9.
DateOfExam Smiley Very Happyate7.
Grade :5.2
Result :$;
datalines;
George.Math.19Dec05.1750.P
Susan.Geography.01Feb05.1101.F
;
run;

Regards,

BPD
N/A
Posts: 0

Re: Handling '.' as a delimiter

Thanks BPD. That seems like a better way to handle these kind of ambiguities.
Ramin
Valued Guide
Posts: 2,174

Re: Handling '.' as a delimiter

In this statement [pre] input StudentName :$8.
CourseName :$9.
DateOfExam Smiley Very Happyate7.
Grade :5.2
Result :$; [/pre] remove the explicit/implied decimal 2 from the grade informat. Then it would be like[pre] Grade :5. [/pre]
Much safer I feel,is to predefine stored variable lengths (and then informats) before the INPUT statement, which won't then need informats defined at all.

Good Luck

PeterC just spelling!

Message was edited by: Peter.C
Ask a Question
Discussion stats
  • 6 replies
  • 116 views
  • 0 likes
  • 6 in conversation