The IEEE 802.11ah standard has been released to support machine-to-machine (M2M) communications among many sensor devices, which is rapidly increasing with the Internet of Things (IoT). The medium access control of 802.11ah introduces a restricted access window (RAW) into each beacon interval (BI). The RAW is further divided into many slots that allow only sensor devices to contend for transmission. This paper proposes a dynamic slot allocation scheme (DSAS) in the RAW of 802.11ah networks. To alleviate the contention in crowded M2M environments, the DSAS defines two sets of thresholds, high and low, to determine the sleeping-time level of a machine and the contention level for a slot, respectively. The innovative aspect of the DSAS is that machines with short sleeping time are allocated to less congested slots whereas machines with long sleeping time are allocated to more congested slots. To avoid unnecessary slot reallocation, machines with sleeping-time levels or contention levels between the high and low thresholds continue to use their original transmission contention slots. Simulations performed on ns-3 show our proposed DSAS has significantly improved average backoff time and packet delay and increased the overall system throughput.