Virata manual

append

Stacks another file underneath this one.

What we have

  • several files at once
  • generate() to mark which file each row came from, and keep()
  • a variable missing from one file is filled with missing, not dropped

Coming later

Nothing known to be missing.

How you write it

append using "file.dta" ["more.dta" ...] [, generate(var)]

Square brackets mean optional. A vertical bar means choose one.

What it does

For when the same information arrives in pieces — one file per year, one per district, one per field office. append puts them end to end into one dataset.

Columns are matched by name. A column that exists in one file and not the other is kept, and filled with blanks where there is nothing to put. Add generate(source) and you get a column saying which file each row came from, which you will want more often than not.

Watch out. Columns are matched by *name*, so Age and age become two different columns. Check with describe afterwards.

Try it

Every example below was run after sysuse vira.sample03, clear.

. keep in 1/10
(3,990 observations deleted)
The same thing in pandas and in R

pandas

import numpy as np

mask = (np.arange(1, len(df) + 1) >= 1) & (np.arange(1, len(df) + 1) <= 10)
df = df[mask].reset_index(drop=True)

R

library(dplyr)
df <- df %>% filter(row_number() >= 1 & row_number() <= 10)

# This R was run in a real R (4.6.1) and gave the same numbers.
. save "part.dta", replace
file part.dta saved
The same thing in pandas and in R

pandas

df.to_stata("part.dta", write_index=False)

R

library(haven)
write_dta(df, "part.dta")

# This R has NOT been run against a real R yet: read it as documentation.
. append using "part.dta"
(10 observations appended from 1 file)
The same thing in pandas and in R

pandas

import pandas as pd

df = pd.concat([df] + [pd.read_stata(p) for p in ['part.dta']], ignore_index=True)

R

library(dplyr)
library(haven)
df <- bind_rows(df, read_dta("part.dta"))

# This R was run in a real R (4.6.1) and gave the same numbers.

Nearby

merge joinby cross collapse contract reshape