Home > Data Management > Importing Facebook data into Stata

Importing Facebook data into Stata

As of 2018, this command no longer works due to Facebook API restrictions.

In a previous post, we released a new command to import Twitter data into Stata. We have now added another new command, facebook2stata, that imports Facebook data. To install facebook2stata, type

net install https://www.stata.com/users/kcrow/facebook2stata, replace

Once installed, you can do the following

  • Import event data using a search string
  • facebook2stata searchevents "search_string"
  • Import group data using a search string
  • facebook2stata searchgroups "search_string"
  • Import page data using a search string
  • facebook2stata searchpages "search_string"
  • Import place data using a search string
  • facebook2stata searchplaces "search_string"
  • Import user data using a search string
  • facebook2stata searchusers "search_string"

Like twitter2stata, there is some required setup with your Facebook account to make this command work in Stata. You must make your own Facebook app for facebook2stata to work. The steps are as follows:

  1. To use this command, you must have a Facebook account. If you don’t have one, you can create one here.
  2. Next, login to your Facebook account and go to here. Click on the Create a New App button.


  3. Next, give your app a name and email address.


  4. Now, you will need to copy the generated App ID and App Secret to a do-file. You will need both of them to use the command.


    Next, click on the Tools & Support menu.

  5. You should now seegraph1
  6. Now, click on the Access Token Tool button.
  7. On this page, you need to click the need to grant permissions link to generate your User Token.


  8. You will need to copy the User Token to a do-file.


  9. NOTE: The User Token that was generated is a short-term token lasting only a few days. You can extened the token for 60 days by clicking on the Debug button. You should see


    To extend the token, click on the Extend Access Token button.

Unlike twitter2stata, the data available to import from Facebook are somewhat limited. The main limitation of Facebook data is that if you want to access a person’s data, they must give your Facebook app permission to do so. The event, group, place, and page data have less restriction.

Again, you will want to copy the

  • App ID
  • App Secret
  • User Token

and paste them into a do-file, for example,

local user_token "74741598400768-3hAYpZbiDvABPizx5lk57B8CTVyfa"
local app_secret "7D25oVzWeDCHrUlQcp9929@GOcnqWCuUKhDel"
local app_id "xWNlx*N9vESv0ZZBtGdm7fVB"

Be sure not to share these with anybody else.

In the same do-file, add the command

facebook2stata setaccess "`user_token'" "`app_id'" "`app_secret'"

to initialize these settings for facebook2stata. If you don’t use facebook2stata setaccess … before each facebook2stata session, you will receive the error below:

. facebook2stata searchgroups "star wars", count(10)
  user token, app id, or app secret not set.
  Run facebook2stata setaccess to set your user token, app id, and app secret.

My do-file is now

local user_token "74741598400768-3hAYpZbiDvABPizx5lk57B8CTVyfa"
local app_secret "7D25oVzWeDCHrUlQcp9929@GOcnqWCuUKhDel"
local app_id "xWNlx*N9vESv0ZZBtGdm7fVB"

facebook2stata setaccess "`user_token'" "`app_id'" "`app_secret'"
facebook2stata searchgroups "star wars", count(10)

When I run the do-file, I get

. facebook2stata searchevents "star wars", count(10)
(7 vars, 10 obs)
. list group_name  group_owner_name

     |                            group_name        group_owner_name |
  1. |                             Star Wars           Olívio Farias |
  2. |                      Star Wars Brasil   João Carlos Damasceno |
  3. |                  STAR WARS GRUPO FANS           Ervin Ramirez |
  4. |            STAR WARS - Greek Fan Club         Stelios Kourtis |
  5. | Star Wars Coleccionistas / Collectors                 Moi Rdz |
  6. |                 Star Wars Sithposting                         |
  7. |                    Star Wars Universe            David Alonso |
  8. |      STAR WARS: Anything & Everything            Steve Sabbai |
  9. |                   Star Wars Verrückte          Frank Lichters |
 10. |                 Star Wars Fans Italia                         |

There are limits to the amount of data Facebook will let you import. These limits are subcommand-specific and limit the number of calls you can make to Facebook’s webpage. Click here to see the data rate limits for the Graph API.

If you have any other social media data you would like to import, feel free to post your suggestion in our comments. You can read the full details of facebook2stata‘s functionality in its help file after installing it.

Categories: Data Management Tags: ,