How can I use the hlm program to convert a Stata file to an HLM file?
It can be tricky to get your data into HLM, but there is a great Stata program called hlm that makes this process pretty painless. You can download hlm by typing search hlm.
VERY IMPORTANT! The Stata hlm program calls HLM 6.x to convert your Stata file to an HLM file, so you need to first set your path variable so the HLM program can be found. It is very important to complete this step, otherwise the conversion will not work. You need to do this only once. Also, as of hlm version 2.0, hlm only works with HLM version 6.x and reads Stata data files from version 11 and lower.
Example 1: A 2-level univariate model
use https://stats.idre.ucla.edu/stat/hlm/faq/hsball, clearThis file has a 2-level structure, students nested in schools. The variable id is the school id.
describeContains data from https://stats.idre.ucla.edu/stat/hlm/faq/hsball.dta obs: 7,185 vars: 11 17 Oct 2002 09:34 size: 244,290 (76.1% of memory free) ------------------------------------------------------------------------------- storage display value variable name type format label variable label ------------------------------------------------------------------------------- id str4 %4s size int %9.0g sector byte %9.0g pracad float %9.0g disclim float %9.0g himinty byte %9.0g meanses float %9.0g minority byte %9.0g female byte %9.0g ses float %9.0g mathach float %9.0g ------------------------------------------------------------------------------- Sorted by: idtab idid | Freq. Percent Cum. ------------+----------------------------------- 1224 | 47 0.65 0.65 1288 | 25 0.35 1.00 1296 | 48 0.67 1.67 1308 | 20 0.28 1.95 1317 | 48 0.67 2.62 1358 | 30 0.42 3.03 1374 | 28 0.39 3.42 1433 | 35 0.49 3.91 1436 | 44 0.61 4.52 1461 | 33 0.46 4.98 1462 | 57 0.79 5.78 1477 | 62 0.86 6.64 1499 | 53 0.74 7.38 1637 | 27 0.38 7.75 1906 | 53 0.74 8.49 1909 | 28 0.39 8.88 1942 | 29 0.40 9.28 1946 | 39 0.54 9.83 2030 | 47 0.65 10.48 2208 | 60 0.84 11.32 2277 | 61 0.85 12.16 2305 | 67 0.93 13.10 (more output omitted here)
Now let’s say we are interested in a 2-level model where our level-1 variables are mathach, ses and female and our level-2 variables are meanses, sector and size and we want to call our new HLM file myhsb.mdm . The option id() is required to specify the hierarchical structure and the l1() and l2() options specify the level-1 variables and level-2 variables in the model.
hlm mkmdm using myhsb, id(id) l1(mathach ses female) l2(size sector meanses)file myhsb.dta saved (note: file myhsb.sts not found) HLM .mdmt file myhsb.mdmt saved. HLM .mdm file myhsb.mdm saved. HLM .sts file myhsb.sts saved.
You can now start HLM and click File then Create a New Model Using an Existing MDM File and open the file myhsb.mdm.
Example 2: A 3-level univariate model
use https://stats.idre.ucla.edu/stat/hlm/faq/egall3, clear
This data file has a 3-level structure. The repeated observations on a children’s standardized math scores and children are nested in schools.
describeContains data from https://stats.idre.ucla.edu/stat/hlm/faq/egall3.dta obs: 7,230 vars: 12 6 Nov 2002 10:04 size: 491,640 (50.0% of memory free) (_dta has notes) ------------------------------------------------------------------------------- storage display value variable name type format label variable label ------------------------------------------------------------------------------- schoolid str12 %12s childid str12 %12s female float %9.0g black float %9.0g hispanic float %9.0g size float %9.0g lowinc float %9.0g mobility float %9.0g year float %9.0g grade float %9.0g math float %9.0g retained float %9.0g ------------------------------------------------------------------------------- Sorted by: schoolid childidlist schoolid childid math in 1/40schoolid childid math 1. 2020 273026452 1.134 2. 2020 273026452 1.146 3. 2020 273026452 2.3 4. 2020 273030991 -1.303 5. 2020 273030991 2.43 6. 2020 273030991 2.254 7. 2020 273030991 3.873 8. 2020 273030991 .439 9. 2020 273059461 -1.384 10. 2020 273059461 3.055 11. 2020 273059461 1.146 12. 2020 273059461 .338 13. 2020 273059461 1.839 14. 2020 278058841 2.814 15. 2020 278058841 -.733 16. 2020 278058841 1.043 17. 2020 278058841 3.544 18. 2020 278058841 2.14 19. 2020 292017571 2.79 20. 2020 292017571 .148 21. 2020 292017571 -.733 22. 2020 292020281 -1.13 23. 2020 292020281 2.035 24. 2020 292020281 .021 25. 2020 292020281 .463 26. 2020 292020281 -.921 27. 2020 292020281 -3.068 28. 2020 292020361 .227 29. 2020 292020361 .403 30. 2020 292020361 -2.732 31. 2020 292020361 1.623 32. 2020 292020361 -.988 33. 2020 292020361 -2.097 34. 2020 292025081 -1.83 35. 2020 292025081 3.055 36. 2020 292025081 .716 37. 2020 292025081 -.785 38. 2020 292025081 1.202 39. 2020 292026211 -1.541 40. 2020 292026211 -.522
To create an HLM file for a 3-level model, we need to specify both level-2 and level-3 id variables. For example, to create an HLM file called egall3.mdm, we do:
hlm mkmdm using egall3, type(hlm3) id(childid) id3(schoolid) l1(year grade math) l2( female black hispanic) l3(size lowinc mobility)file egall3.dta saved (note: file egall3.sts not found) HLM .mdmt file egall3.mdmt saved. HLM .mdm file egall3.mdm saved. HLM .sts file egall3.sts saved.
Here we have to specify type(hlm3) option to create a three level file. The id() option gives the level-2 id variable and the id3() option gives the level-3 id variable. The l1(), l2(), and l3() options give the level 1, level 2 and level 3 variables.
You can now start HLM and click File then Create a New Model Using an Existing MDM File and open the file egall3.mdm.