Home  /  Resources & Support  /  Introduction to Stata basics  /  Import data from a website

Sometimes, we would like to work with data that are available on the Internet. It is easy to import data from across the web by specifying a URL in the import command. To demonstrate, we will use import sasxport5 to import a SAS XPORT file containing data from the National Health And Nutrition Examination Survey website.

Go to the website below, right-click on the link labeled "DEMO_I_Data [XPT - 3.6MB]", and select "Copy link".

https://wwwn.cdc.gov/nchs/nhanes/search/datapage.aspx?Component=Demographics&CycleBeginYear=2015

Then, type import sasxport5 and paste the link after the command.

. import sasxport5 https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT

Let's type describe to verify that our data imported successfully.

. describe

Contains data
 Observations:         9,971                  
    Variables:            47 

Variable Storage Display Value
name type format label Variable label
seqn double %10.0g Respondent sequence number sddsrvyr double %10.0g Data release cycle ridstatr double %10.0g Interview/Examination status riagendr double %10.0g Gender ridageyr double %10.0g Age in years at screening ridagemn double %10.0g Age in months at screening - 0 to 24 mos ridreth1 double %10.0g Race/Hispanic origin ridreth3 double %10.0g Race/Hispanic origin w/ NH Asian ridexmon double %10.0g Six month time period ridexagm double %10.0g Age in months at exam - 0 to 19 years dmqmiliz double %10.0g Served active duty in US Armed Forces dmqadfc double %10.0g Served in a foreign country dmdborn4 double %10.0g Country of birth dmdcitzn double %10.0g Citizenship status dmdyrsus double %10.0g Length of time in US dmdeduc3 double %10.0g Education level - Children/Youth 6-19 dmdeduc2 double %10.0g Education level - Adults 20+ dmdmartl double %10.0g Marital status ridexprg double %10.0g Pregnancy status at exam sialang double %10.0g Language of SP Interview siaproxy double %10.0g Proxy used in SP Interview? siaintrp double %10.0g Interpreter used in SP Interview? fialang double %10.0g Language of Family Interview fiaproxy double %10.0g Proxy used in Family Interview? fiaintrp double %10.0g Interpreter used in Family Interview? mialang double %10.0g Language of MEC Interview miaproxy double %10.0g Proxy used in MEC Interview? miaintrp double %10.0g Interpreter used in MEC Interview? aialanga double %10.0g Language of ACASI Interview dmdhhsiz double %10.0g Total number of people in the Household dmdfmsiz double %10.0g Total number of people in the Family dmdhhsza double %10.0g # of children 5 years or younger in HH dmdhhszb double %10.0g # of children 6-17 years old in HH dmdhhsze double %10.0g # of adults 60 years or older in HH dmdhrgnd double %10.0g HH ref person's gender dmdhrage double %10.0g HH ref person's age in years dmdhrbr4 double %10.0g HH ref person's country of birth dmdhredu double %10.0g HH ref person's education level dmdhrmar double %10.0g HH ref person's marital status dmdhsedu double %10.0g HH ref person's spouse's education level wtint2yr double %10.0g Full sample 2 year interview weight wtmec2yr double %10.0g Full sample 2 year MEC exam weight sdmvpsu double %10.0g Masked variance pseudo-PSU sdmvstra double %10.0g Masked variance pseudo-stratum indhhin2 double %10.0g Annual household income indfmin2 double %10.0g Annual family income indfmpir double %10.0g Ratio of family income to poverty
Sorted by: Note: Dataset has changed since last saved.

The dataset contains 9,971 observations and 47 variables. That's all there is to it. Notice that we did not have to copy the file to our working directory before we imported the file. We simply pasted the URL and filename directly into the import command. Now we can save our data to a Stata dataset by typing save mynhanes.

. save mynhanes
file mynhanes.dta saved

You can watch a demonstration of these commands by clicking on the link to the YouTube video below. You can read more about these commands by clicking on the links to the Stata manual entries below.

See it in action

Watch Import data from SPSS and SAS.