Analog Inputs Causing Program to Stop

I think I get it now. I rewrote the program without using the STRING command but still writes to the SD card in the same format.
Thanks for pointing this out to me, Nick.

/*
  THIS PROGRAM WRITES DATA TO AN SD CARD WITHOUT USING
  THE STRING COMMAND.
 */

#include <SD.h>
// On the Ethernet Shield, CS is pin 4. 
const int chipSelect = 4;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
digitalWrite(10,HIGH); // disables w5100 
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile) {
  // read three sensors 
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
      dataFile.print(sensor) ;
        Serial.print(sensor);
        if (analogPin < 2) {  
              dataFile.print(", ") ;
        Serial.print(", ");  
        }    
      else { 
  
       dataFile.println();
        Serial.println();  
          dataFile.close();  
      }       
    }
 }
 
         else {
    Serial.println("error opening datalog.txt");

  }
  delay(500);
}

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)