Stata command to convert string GVKEY to numerical GVKEY or vice versa

The default type of GVEKY in Compustat is string. Sometimes, we need it to be a numerical type in Stata (e.g., when we want to use the super handy command tsset). The command to convert string GVKEY to numerical GVEKY is very simple:

destring gvkey, replace

The command to revert numerical GVKEY to string GVKEY with leading zeros is as follows:

tostring gvkey, replace format(%06.0f)

This entry was posted in Stata. Bookmark the permalink.

1 Response to Stata command to convert string GVKEY to numerical GVKEY or vice versa

  1. Blair says:

    Hi Kai,
    Thank you very much for your sharing. Your code inspires me a lot and I think we can do the same things in SAS for conversion.

    From string GVKEY to numerical GVKEY :
    gvkey_numerical=input(gvkey_string, best12.);

    And from numerical GVKEY to string GVKEY :
    gvkey_string=put(gvkey_numerical, z6.);

    Best Regards,
    Blair

Leave a Reply

Your email address will not be published. Required fields are marked *