Virata manual

tostring

Turns a number column into text.

What we have

  • generate() or replace

Coming later

  • format(), to control how the number is written out

How you write it

tostring varlist, generate(newvars) | replace

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

What it does

Needed more often than you would think: identifiers. A postcode, a phone number or a household ID with leading zeros is not a quantity, and treating it as one loses the zeros and invites arithmetic that means nothing.

Also the fix when two files you want to join hold the same key as a number in one and text in the other — merge refuses that, correctly, and this is one way to make the two agree.

Try it

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

. tostring age, generate(age_text)
age: generated as str
The same thing in pandas and in R

R

# no R form for `tostring` yet -- see docs/r-equivalents.md

Nearby

generate replace egen recode rename order