/* Import Facebook Data into STATA */
First, install module “facebook2stata” module in your STATA software with the command below:
/* Install module to import data from Facebook to STATA. */
net install https://www.stata.com/users/kcrow/facebook2stata, replace
These are commands that you can use to import data from Facebook Events, Groups, Pages, Places and Users. Obtaining data from user requires permission of the user. Saying this, if you see some apps in Facebook asking “Do you give permission to access your personal data?” and request to access name, friends information, location, posting on your profile etc. That is what I meant to say.
- Import event data using a search string:
facebook2stata searchevents “search_string” - Import Group Data Using a ssearch 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”
You may need to create your Facebook app before running this code in STATA. After creating an app on Facebook, you will need “App ID”, “App Secret” and User Token.
How to Create an app on Facebook?
- Go to: Developers for Facebook Apps section.
- Click “Create New App” and give a name.
- Go to setting of that app. You will get your first treasure hunt i.e. App ID. Copy and paste in the .do file in STATA.
- Click on “Tools” section in the webpage. It will take you to another page with one of the options listed as “Access Token Tool:”. Click Access Token Tools and you will find your rest of the treasures i.e. App Token and User Token. You need to grant permission to your app to see app token. Copy App token and User Token and paste in same .do STATA file.
- if you debug tokens, they may last longer than undebugged token.
- Final Step, Run following code below. Don’t forget to replace actual keys in the location where I mentioned the name.
local user_token “…………”
local app_secret “…………”
local app_id “…………….”
Note: this three information should be kept very secure and private. Anyone with this three information above can import data using your app.
facebook2stata setaccess “user_token” “app_id” “app_secret”
facebook2stata searchplaces “Nepal”, count(10) clear
Note: You can keep any search term in the place of “Nepal” and any value in the place of “10” in above code. Instead of “searchplaces” try different options such as mentioned above at the beginning.
This will create data in your STATA. Try listing, tab, sum to explore data. You can now use data in the way you want.
Congratulations! You made it. In next post, I will do a similar blog post based on Twitter. Keep following up.
This Post is created with the help of the previous blog post on STATA website.