360Studies

360 Studies.com Logo

Your Destination for Career Excellence in Bioscience, Statistics, and Data Science

How can you generate a variable in Stata

how-can-you-generate-a-variable-in-stata

Before starting this exercise, you should have the “Digital.dta” file. (Download link is given below)

Click here to download“Digital.dta”

Importing the “Digital.dta” file.

set maxvar 10000
use "/Users/pankajchowdhury/Downloads/Digital.dta"

I have the file located at "/Users/pankajchowdhury/Downloads/Digital.dta". Feel free to modify the file path according to your needs.

Observations:724115
Variables:31
Variable nameStorage typeDisplay formatValue labelVariable label
v005long%12.0gv005women's individual sample weight (6 decimals)
v013byte%8.0gV013age in 5-year groups
v024byte%8.0gV024state
v025byte%8.0gV025type of place of residence
v130byte%8.0gV130religion
v133byte%8.0gV133education in single years
v151byte%8.0gV151sex of household head
v157byte%8.0gV157frequency of reading newspaper or magazine
v158byte%8.0gV158frequency of listening to radio
v159byte%8.0gV159frequency of watching television
v169abyte%8.0gV169Aowns a mobile telephone
v190byte%8.0gV190wealth index combined
v217byte%8.0gV217knowledge of ovulatory cycle
v504byte%8.0gV504currently residing with husband/partner
v702byte%8.0gV702husband/partner's highest year of education (at level in v701)
v704byte%8.0gV704husband/partner's occupation
v715byte%8.0gV715husband/partner's total number of years of education
v730byte%8.0gV730husband/partner's age
v743fbyte%8.0gV743Fperson who usually decides what to do with money husband earns
v746byte%8.0gV746respondent earns more than husband/partner
d005long%12.0gd005weight for domestic violence (6 decimals)
d102byte%8.0gd012number of control issues answered 'yes' (d101x = 1)
sweightlong%12.0gsweightsample weight (6 decimals) (state level)
s116byte%8.0gS116belong to a scheduled caste, a scheduled tribe, other backward class
s303int%8.0gS303time period not living with husband
s311byte%8.0gS311type of relationship to current husband,prior to marriage
s931byte%8.0gS931do you have a bank or savings account that you yourself use
s932byte%8.0gS932do you have any mobile phone that you yourself use
s933byte%8.0gS933do you use your mobile phone for any financial transaction ?
s934byte%8.0gS934have you ever used the internet?
s1004pbyte%8.0gS1004Psource of information about aids: internet

Code 1.1:

codebook v730 
ta v730

Result : codebook v730– This command will check the variable type, variable label, Range, Unique values, value label and frequency distribution of a particular variable “v730”.

codebookv730
v730 :husband/partner's age
Type:Numeric (byte)
Label:V730, but 74 nonmissing values are not labeled
Range:[15,95]Units: 1
Unique values:74Missing :647,033/724,115

Code 1.2:

ta v730

Result :  

husband/partner's ageFreq.PercentCum.
15480.060.06
16110.010.08
17270.040.11
181020.130.24
191510.20.44
203940.510.95
215260.681.63
228461.12.73
239251.23.93
241,3211.715.64
252,2362.98.55
261,8282.3710.92
272,0762.6913.61
282,6003.3716.98
291,9372.5119.5
304,1305.3624.85
311,7362.2527.11
323,2034.1631.26
331,9832.5733.83
342,309336.83
354,7046.142.93
362,3433.0445.97
372,0322.6448.61
382,8593.7152.32
391,9592.5454.86
404,4375.7660.61
411,5251.9862.59
422,5663.3365.92
431,6082.0968.01
441,7192.2370.24
454,1995.4575.69
461,7802.3177.99
471,7522.2780.27
482,3903.183.37
491,6712.1785.54
502,6193.488.93
511,3161.7190.64
521,6732.1792.81
531,0301.3494.15
541,0141.3295.46
558121.0596.52
565950.7797.29
574480.5897.87
584540.5998.46
592660.3598.8
603670.4899.28
61900.1299.4
621060.1499.53
63620.0899.61
64400.0599.67
65780.199.77
66230.0399.8
67190.0299.82
68170.0299.84
69130.0299.86
70290.0499.9
7140.0199.9
723099.91
7340.0199.91
7450.0199.92
752099.92
761099.92
771099.92
781099.93
791099.93
8050.0199.93
823099.94
831099.94
841099.94
851099.94
871099.94
881099.94
901099.95
95420.05100
Total77,082100

Explanation : ta v730– this command will provide a frequency table comprising cumulative frequency and percentage distribution for variable v730.

Code 2:

gen five_year = 5
ta five_year

Explanation : gen five_year = 5  this command will create a variable that assigns the value of “5” to each observation.

Code 3:

gen int age_plus_five = v730 + five_year 
ta age_plus_five

Explanation : gen int age_plus_five = v730 + five_year– create a fresh variable ( variable type: int) by incrementing each value in the variable v730 by 5.

Result :

age_plus_five (v730+ Five year)Freq.PercentCum.
20480.060.06
21110.010.08
22270.040.11
231020.130.24
241510.20.44
253940.510.95
265260.681.63
278461.12.73
289251.23.93
291,3211.715.64
302,2362.98.55
311,8282.3710.92
322,0762.6913.61
332,6003.3716.98
341,9372.5119.5
354,1305.3624.85
361,7362.2527.11
373,2034.1631.26
381,9832.5733.83
392,309336.83
404,7046.142.93
412,3433.0445.97
422,0322.6448.61
432,8593.7152.32
441,9592.5454.86
454,4375.7660.61
461,5251.9862.59
472,5663.3365.92
481,6082.0968.01
491,7192.2370.24
504,1995.4575.69
511,7802.3177.99
521,7522.2780.27
532,3903.183.37
541,6712.1785.54
552,6193.488.93
561,3161.7190.64
571,6732.1792.81
581,0301.3494.15
591,0141.3295.46
608121.0596.52
615950.7797.29
624480.5897.87
634540.5998.46
642660.3598.8
653670.4899.28
66900.1299.4
671060.1499.53
68620.0899.61
69400.0599.67
70780.199.77
71230.0399.8
72190.0299.82
73170.0299.84
74130.0299.86
75290.0499.9
7640.0199.9
773099.91
7840.0199.91
7950.0199.92
802099.92
811099.92
821099.92
831099.93
841099.93
8550.0199.93
873099.94
881099.94
891099.94
901099.94
921099.94
931099.94
951099.95
100420.05100
Total77,082100

Code 4:

gen int age_plus_five2 = v730 + five_year if v730 > 25 ta age_plus_five2

Explanation : gen int age_plus_five2 = v730 + five_year if v730 > 25 – When the age variable (v730) exceeds 30 years, executing this command will produce a new variable by incrementing each observation in v730 by five.

Results :

age_plus_five2Freq.PercentCum.
311,8282.592.59
322,0762.945.54
332,6003.699.23
341,9372.7511.97
354,1305.8617.83
361,7362.4620.3
373,2034.5424.84
381,9832.8127.65
392,3093.2830.93
404,7046.6737.6
412,3433.3240.92
422,0322.8843.81
432,8594.0647.86
441,9592.7850.64
454,4376.2956.93
461,5252.1659.1
472,5663.6462.74
481,6082.2865.02
491,7192.4467.46
504,1995.9673.41
511,7802.5375.94
521,7522.4978.42
532,3903.3981.81
541,6712.3784.18
552,6193.7287.9
561,3161.8789.77
571,6732.3792.14
581,0301.4693.6
591,0141.4495.04
608121.1596.19
615950.8497.04
624480.6497.67
634540.6498.31
642660.3898.69
653670.5299.21
66900.1399.34
671060.1599.49
68620.0999.58
69400.0699.64
70780.1199.75
71230.0399.78
72190.0399.81
73170.0299.83
74130.0299.85
75290.0499.89
7640.0199.9
773099.9
7840.0199.9
7950.0199.91
802099.91
811099.92
821099.92
831099.92
841099.92
8550.0199.93
873099.93
881099.93
891099.93
901099.94
921099.94
931099.94
951099.94
100420.06100
Total70,495100

Code 5:

gen age2 = v730^2
ta age2

Explanation : gen age2 = v730^2– By squaring the variable values, this command will create a new variable.

Results : 

age2Freq. PercentCum.
225480.060.06
256110.010.08
289270.040.11
3241020.130.24
3611510.20.44
4003940.510.95
4415260.681.63
4848461.12.73
5299251.23.93
5761,3211.715.64
6252,2362.98.55
6761,8282.3710.92
7292,0762.6913.61
7842,6003.3716.98
8411,9372.5119.5
9004,1305.3624.85
9611,7362.2527.11
10243,2034.1631.26
10891,9832.5733.83
11562,309336.83
12254,7046.142.93
12962,3433.0445.97
13692,0322.6448.61
14442,8593.7152.32
15211,9592.5454.86
16004,4375.7660.61
16811,5251.9862.59
17642,5663.3365.92
18491,6082.0968.01
19361,7192.2370.24
20254,1995.4575.69
21161,7802.3177.99
22091,7522.2780.27
23042,3903.183.37
24011,6712.1785.54
25002,6193.488.93
26011,3161.7190.64
27041,6732.1792.81
28091,0301.3494.15
29161,0141.3295.46
30258121.0596.52
31365950.7797.29
32494480.5897.87
33644540.5998.46
34812660.3598.8
36003670.4899.28
3721900.1299.4
38441060.1499.53
3969620.0899.61
4096400.0599.67
4225780.199.77
4356230.0399.8
4489190.0299.82
4624170.0299.84
4761130.0299.86
4900290.0499.9
504140.0199.9
51843099.91
532940.0199.91
547650.0199.92
56252099.92
57761099.92
59291099.92
60841099.93
62411099.93
640050.0199.93
67243099.94
68891099.94
70561099.94
72251099.94
75691099.94
77441099.94
81001099.95
9025420.05100
Total77,082100

Code 6:

gen age = sqrt(age2) ta age

Explanation :

gen age = sqrt(age2) – This command will create a new variable by substituting the variable values with their square root.

Results:

ageFreq.PercentCum.
15480.060.06
16110.010.08
17270.040.11
181020.130.24
191510.20.44
203940.510.95
215260.681.63
228461.12.73
239251.23.93
241,3211.715.64
252,2362.98.55
261,8282.3710.92
272,0762.6913.61
282,6003.3716.98
291,9372.5119.5
304,1305.3624.85
311,7362.2527.11
323,2034.1631.26
331,9832.5733.83
342,309336.83
354,7046.142.93
362,3433.0445.97
372,0322.6448.61
382,8593.7152.32
391,9592.5454.86
404,4375.7660.61
411,5251.9862.59
422,5663.3365.92
431,6082.0968.01
441,7192.2370.24
454,1995.4575.69
461,7802.3177.99
471,7522.2780.27
482,3903.183.37
491,6712.1785.54
502,6193.488.93
511,3161.7190.64
521,6732.1792.81
531,0301.3494.15
541,0141.3295.46
558121.0596.52
565950.7797.29
574480.5897.87
584540.5998.46
592660.3598.8
603670.4899.28
61900.1299.4
621060.1499.53
63620.0899.61
64400.0599.67
65780.199.77
66230.0399.8
67190.0299.82
68170.0299.84
69130.0299.86
70290.0499.9
7140.0199.9
723099.91
7340.0199.91
7450.0199.92
752099.92
761099.92
771099.92
781099.93
791099.93
8050.0199.93
823099.94
831099.94
841099.94
851099.94
871099.94
881099.94
901099.95
95420.05100
Total77,082100

Looking for latest updates and job news, join us on Facebook, WhatsApp, Telegram and Linkedin

You May Also Like

Scroll to Top