This paper presents an effective method to control the state of charge (SOC) of energy storage system (ESS) installed in a microgrid (MG). Considering voltage and frequency variations after islanding occurrence and based on the stability criteria, MG pre-islanding conditions are divided into secure and insecure classes. It is shown that insecure MG can become secure, if appropriate value for SOC of the ESS is chosen in different conditions of the MG. To select the most important variables of MG, which can estimate proper values of the SOC, a feature selection procedure known as RELIEF is used in this paper. Among all the MG variables, critical ones are selected. Using them, appropriate value of the SOC can be calculated for different conditions of the MG. This approach is economic because it does not change the result of optimal power flow (OPF). In some conditions, the proper control of SOC cannot solely make the MG secure. In these conditions, DGs generated power should be also changed to make the MG secure. This condition leads to a violation from OPF solution and increases the cost of operation. The results demonstrate the effectiveness of the proposed method in comparison with other methods.