Sending SMS message vis GPRS SIM900A with Arduino UNO is not reliable

that's an example I wrote some time back. May be it can give you some ideas

enum t_status : uint8_t {ONHOLD, WAITING, OK_FOUND, TIMEOUT};
t_status waitForOK(bool getReady = false, uint32_t waitingTime = 2000ul) {
  static char buffer[2];
  static uint32_t startTime;
  static uint32_t timeout;
  static t_status currentState = ONHOLD;

  if (getReady) { // prepare to receive
    startTime = millis();
    buffer[0] = buffer[1] = '\0';
    timeout = waitingTime;
    currentState = WAITING;
    return WAITING;
  } else {
    if ((currentState != ONHOLD) && (millis() - startTime >= timeout)) {
      currentState = ONHOLD;
      return TIMEOUT;
    }
    if (Serial.peek() == -1) return currentState;

    if (currentState == WAITING) {
      buffer[0] = buffer[1];
      buffer[1] = Serial.read();
      if (strncmp(buffer, "OK", 2) == 0) {
        currentState = ONHOLD;
        return OK_FOUND;
      }
    }
  }
  return currentState;
}

void setup() {
  Serial.begin(115200);
  Serial.println("\nYou have 5s to type OK");
  waitForOK(true, 5000);
}

void loop() {
  switch (waitForOK()) {
    case ONHOLD: break;
    case WAITING: break;
    case OK_FOUND:
      Serial.println("I got OK.");
      if (Serial.available()) {
        Serial.println("There was some stuff in the incoming buffer after the OK");
        while (Serial.available()) {
          char r = Serial.read();
          Serial.print("0x");
          Serial.print(r, HEX);
          if (isalnum(r)) {
            Serial.print("\t('");
            Serial.write(r);
            Serial.println("')");
          } else if (r == '\n') Serial.println("\t(NL)");
          else if (r == '\r') Serial.println("\t(CR)");
          else Serial.println();
        }
      }
      Serial.println("Done");
      break;
    case TIMEOUT:
      Serial.println("I did not get OK, timeout");
      Serial.println("You have 5s to type OK, try again");
      waitForOK(true, 5000);
      break;
  }
}

run it with Serial monitor at 115200 bauds

it will ask you to type "OK" in the Serial console. You can type other stuff, it will only stop when it gets to OK. when it gets "OK" it will report also what's left in the Serial buffer (by reading it)