1. 首页 > 手机 >

stata字符串改数值 stata中字符串改成数字

split — 将字符串变量拆分为多部分

}//shenme yisi

split 使用一个或多个解析字符串(默认情况下为空格)将字符串变量 strvar 的内容拆分为一个或多个部分,以便生成新的字符串变量。 因此,拆分对于分离“字”或字符串变量的其他部分很有用。 strvar 本身没有修改。

stata字符串改数值 stata中字符串改成数字stata字符串改数值 stata中字符串改成数字


sysuse auto.dta, clear

为 v 的每个单词创建变量v#,用空格分隔

replace make="AMC" in 1

同上所述,但在逗号上分成单词或短语并生成变量 newv#

同上所述,但不要修剪前导或尾随空格

无论可能的新变量数量是多少,都只创建newv1,newv2和newv3

同上所述,并尽可能转换为数字类型

generate(stub) :用 stub 开始新的变量名; 默认是strvar。

parse(parse_strings):解析指定的字符串; default是解析空格。

limit(#) :最多创建#new变量

notrim :不修剪原始变量的前导或尾随空格。

destring:将 destring 应用于新的字符串变量,尽可能用数字变量替换初始字符串变量

ignore("chars"):删除指定的非数字字符强制将非数字字符串转换为缺失值。

float: 生成数值变量为float类型。

percent :将百分比变量转换为小数形式。

generate(stub) 指定新变量名的起始字符,以便生成新变量 stub1 ,stub2 等。 stub 默认为 strvar 。

parse(parse strings) 指定解析使用一个或多个解析字符串,而不是使用空格。 最常见的是,将指定一个标点字符串。 例如,如果指定了parse(,),则“1,2,3”被分成“1”,“2”和“3”。

您还可以指定1)两个或多个字符串,它们是“单词”的替代分隔符和2)由两个或多个字符组成的字符串。 替代字符串应以空格分隔。

包含空格的字符串应该用“”绑定。 因此,如果指定了解析(,“ ”),则“1,2 3”也被分成“1”,“2”和“3”。 特别注意解析(a b)和解析(ab)之间的区别:个,a和b都可以作为分隔符,而对于第二个,只有字符串ab是可接受的。

limit(#) 您还可以指定1)两个或多个字符串,它们是“单词”的替代分隔符和2)由两个或多个字符组成的字符串。 替代字符串应以空格分隔

notrim 指定在解析之前不对前导和尾随空格修剪原始字符串变量。

notrim 与空格上的解析不兼容,因为后者意味着要丢弃字符串中的空格。 您可以指定解析字符,或者默认情况下允许修剪。

destring: 将destring应用于新的字符串变量,尽可能用数字变量替换最初作为字符串创建的变量See [D] destring。

ignore(), force, float, percent; see [D] destring.

希望在做更多分析之前进行细分。

在给定一个或多个分隔符的情况下,split 应用的基本步骤是在字符串中查找这些分隔符,然后生成一个或多个新的字符串变量,每个变量都包含原始的一部分。 分隔符可以是例如空格或其他标点符号,但它们又可以是包含多个字符的字符串。 默认分隔符是空格。

用于细分字符串变量的键字符串函数,实际上通常是字符串,是str(),它查找分隔符的位置,substr(),它提取字符串的一部分。 (参见[FN]字符串函数。)split 是基于这些函数的使用。

如果没有通过拆分分隔符来定义问题,则可能需要直接使用substr()。 假设您有一个字符串变量 date ,其中包含“21011952”形式的日期,以便四个字符定义一年。 该字符串不包含分隔符。 要提取年份,您将使用substr(date,-4,4)。 再次假设过去12个月中每个女性的产科病史都是由含有“nppppppppppbn”等值的str12变量记录的,其中p,b和n表示怀孕,分娩和非怀孕的月份。 再一次,没有分隔符,所以你可以使用 substr()来细分字符串。

split会丢弃分隔符,因为它假定分隔符与进一步分析无关,或者您可以随意恢复它们。如果这不是您想要的,您可以使用 substr() (和

,在我们转向示例之前,将 split 与 egen 函数 ends()进行比较,它生成字符串的头部,尾部或部分。 与所有egen函数一样,此函数仅生成一个新变量。 相反,split 作为一个命令的结果通常会产生几个新变量。 有关更多详细信息和讨论,包括对识别个人姓名的特殊问题的评论, see [D] egen.

当 Stata 的输入以某种方式误读为一个字符串变量时,split可能很有用。 如果您使用剪贴板并粘贴到数据编辑器中,例如,在Windows下,但数据是以空格分隔的,您认为单独的变量将被组合,因为数据编辑器需要逗号或制表符分隔的数据。 如果复合变量的某些部分应该是数字字符放入数值变量,你可以同时使用 destring ; 见[D] destring。

这里没有指定generate()选项,因此新变量将具有名称 var11 ,var12 等。 您现在可能希望使用重命名来生成更具信息性的变量名称。 See[D] rename.

您还可以使用拆分来细分真正的复合材料。 例如,电子邮件地址(例如 tech-support@stat )可能会拆分为“@”:

此序列产生两个新变量:address1,包含“@”之前的电子邮件地址的一部分,例如“tech-support”,以及包含“@”之后的部分的 address2,例如 “ stat ” 。 分隔符本身“@”被丢弃。 由于未指定generate(),因此名称地址在命名新变量时用作存根。 split 显示创建的新变量的名称,因此您可以快速查看创建的数字是否符合您的期望。

如果个人的详细信息没有意义,而您只想要机器名称

or

接下来假设一个字符串变量包含应该分为原告和被告变量的法律案件的名称。 分隔符可以是 “V”,“V”,“VS”和“VS.”。(我们假设使用大写和小写的任何不一致都由字符串函数 strupper()处理; 请参阅[FN]字符串函数。)特别注意我们的前导和尾随空格 分隔符的详细说明:个分隔符是“V”,例如,不是“V”,它会错误地将“GOLIATH V DAVID”分成“GOLIATH”,“DA”和“ID”。 给出了替代分离器作为parse()的参数:

再次使用变量的默认命名并调用分隔符被丢弃,我们期望新变量case1和case2,而不创建case3或其他新变量。 每当找不到指定的分隔符时,case2将具有空值,因此我们可以检查:

假设一个字符串变量包含由制表符分隔的字段。 例如,导入分隔的选项卡保持不变。 知道选项卡是char(9),我们可以输入

p(char(9))不起作用。 parse()的参数是字面意思,但是动态函数的评估可以作为宏替换的一部分。

,假设一个字符串变量包含在括号中绑定的子字符串,如(1 2 3)(4 5 6)。例如,。分割数据,p (“)”)

split stores the following in r():

Scalars

r(nvars) number of new variables created

r(varlist) names of the newly created variables

假设输入在某种程度上被误读为一个字符串变量,例如,当您并粘贴到数据编辑器中时,数据是以空格分隔的:

假设一个字符串变量包含法律案件的名称,应将其分成原告和被告的变量。 分隔符可以是“ V ”,“V”,“VS”和“VS.”。 特别注意我们的分隔符细节中的前导和尾随空格:个分隔符是“ V ”,例如,不是“V”,它会错误地将“GOLIATH V DAVID”分成“GOLIATH”,“DA”和“ID” ”。 替代分隔符作为parse()的参数给出:

问题的迹象是创建两个以上的变量和任何具有空值的变量,因此请检查:

假设字符串变量包含由制表符分隔的字段。 例如,导入分隔的选项卡保持不变。 知道选项卡是char(9),我们可以输入

p(char(9))不起作用。 parse()的参数是字面意思,但是动态函数的评估可以作为宏替换的一部分。

假设一个字符串变量包含括在括号中的子串,例如(1 2 3)(4 5 6)。 在这里,我们可以拆分右括号,如果需要,可以在之后替换它们。 例如,

将 var1 拆分为两个字符串变量,基于“ ”(空格)作为解析字符

删除新创建的变量 var11 和var12

将 var1 拆分为两个变量,基于“ ”作为解析字符,并将变量命名为 geog1和 geog2

使用逗号作为解析字符将 var1 拆分为两个变量,并将变量命名为geog1 和 geog2

使用逗号后跟空格和空格作为解析字符将日期拆分为变量,并使用ndate 作为新变量名称的前缀

使用逗号作为解析字符将x拆分为变量,并尝试用数字变量替换新的字符串变量

可以不学习stata,只学习r语言吗

/ Lecture 1: How to import and export data /

//备注不长于一行可用双斜杠

sysuse "auto.dta",clear

//导入系统自带的数据,clear:关闭之前所有的数据

//字符串变量:红颜色的文字变量。蓝颜色的domestic对应0,foreign对于1

//export data

export delimited /stata 自带的帮助文档,只导出csv和//import txt filetxt格式/

/导出其中部分变量

export delimited [你想导出的变量名称] using filename [if] [in] [,

export delim using"auto.txt",replace

export delim make pr using "auto.csv",replace

//MAKE 和pr是变量名,只导出这两个变量

import excel

export excel using "auto.xlsx" in 11/L

export excel using "auto.xlsx" if pr>=4000,replace

//if: 满足某种条件的变量

export excel using "auto.xlsx" firstrow(var),replace

use "D:\BaiduNetdiskDownload\stata application\Data_ma.dta"

unicode

clear

unicode encoding set gb18030

unicode translate "Data_ma.dta"

use "Data_ma.dta",clear

//打开大数据

set excelxlsxlargefile on

//允许stata打开大文件

//如果内存太小,如何拆分大文件

findit cky

cky

cky using "数据文件名称", peek(1)

//看一下数据,不导入,(1)表示行

//分析

cky using 数据文件名称 ,cksize(10m) header(include) stub(数据文件名称)replace

import delimited using "其中一个数据文件名称”,clear encoding(数据)

//调用拆分后的其中一个数据

sysuse auto.dta,clear

//调用系统数据

export delim "auto1.csv" in 1,replace

export delim "auto2.csv" in 2, replace

export delim "auto3.csv" in 3,replace

export delim "auto4.csv" in 4, replace

export delim "auto5.csv" in 5/L, replace

foreach

foreach num of numlist 1/3 5 8 9(10) 100{

display `num'

}//重新导入数据

foreach num of numlist 1/74{

display "auto `num'"

se "auto`num'.dta", replace

} append

append using "auto2.csv" "auto3.csv""auto4.csv"/

/"auto5.csv"

use "auto1.dta", clear

foreach num of numlist 2/5{

append using "auto`num'.dta"

}se"auto_new.dta", replace

ssc install openall

findit openall

openall, auto?, insheet

//删除多余文件

foreach x of numlist 1/5{

erase"auto`x'.csv"

}//lecture 3

cd "D:\BaiduNetdiskDownload\stata application"

format

sysuse auto.dta

//改变字符串的长度,为了方便浏览

format %30s make

//浏览

edit make

//显示字符串和数值变量

edit make pr headroom

format %-20s make

//小数点后面两位数

format %3.2f headroom

//

format %10.0g pr

list make pr headroom in 1/10

//变量的标签

//介绍数据集的基本情况

//改变整个数据的标签

label data "US auto data美国汽车数据"

//改变变量标签

label var pr "auto pr汽车价格"

//修改虚拟变量标签,先定义后标签

label define origin_v 0 "国产" 1 "进口"

label values foreign origin_v

replace foreign = 2 in 1/8

//每一次定义新标签要重新命名origin

label define origin_new2 0 "国产" 1 "进口" 2 "unknown"

//显示

list make if foreign == 0 //一个等号表示赋值

list make pr if make =="AMC Concord" /

/ | make =="Merc. Cougar"

list make foreign pr if (foreign == 1& pr <=5000) /

/ | (foreign == 0 & pr >3000)

list make pr if inlist (make, "AMC Concord" "Merc. cougar", )

sysuse auto.dta , clear

export excel using "auto.xlsx", nolabel replace

import excel using "auto.xlsx", clear

//修改变量名称

renamsplit用于将字符串变量拆分为两个或多个组件部分,例如“words”。 您可能需要更正错误,或者字符串变量可能是您的真正复合e

rename A pr

rename _all, proper//让变量首字母大写,剩下字母小写

//将变量名称批量写入标签, 重要的是所有的变量循环_all

foreach v of varlist _all {

label variable `v' " `v' "

generate//建立新变量

cd "D:\BaiduNetdiskDownload\stata application"

gen pr2=pr^2 //生产pr2的平方

gen pr_mpg = prmpg if foreign == 1 //生成pr 和mpg的交叉项 如果foreign=1

replace pr_mpg =0 if pr_mpg == .

//用0取代缺失值

gen logpr = log(pr) //生产pr的对数指

gen lnpr = ln(p) //生成pr的自然对数指,其实和上行生成的结果一样的

//出现零的时候取对数会成为missing数据,可能会丢失数据

replace pr_mpg = prmpg //生成pr和mpg的交叉项,并取代pr_mpg变量

gen prcateg = 0//生产prcateg变量,并将数据分组

replace prcateg = 1 if pr >=5000 & pr < 10000

replace prcateg = 2 if pr >= 10000

label values prcateg category

egen

egen prg3 = mean(pr)

gen pr_dev = pr - prg3

//如何分别计算foreign和domestic的均值

sort foreign

//方法一

egen pr_g2 = mean(pr) if foreign == 0

drop pr_g2

//方法二

by foreign: egen prg_数值变量资料的一般分析:参数估计,t检验,单因素和多因素的方分析,协方分析,交互效应模型,平衡和非平衡设计,嵌套设计,随机效应,多个均数的两两比较,缺项数据的处理,方齐性检验,正态性检验,变量变换等。by = mean(pr)//按照foreign的分类做均值

sort foreign //给foreign按数据大小排序

tostring // 数值变量变字符串

destring //字符串变数值变量,字符串不可以做运算的

edit mpg

tostring mpg, gen(mpg_str)

tostring mpg, replace force//不想产生新的变量,有mpg取代原来的

edit mpg mpg_str

destring mpg_str, replace force//将字符串变数值

edit mpg mpg_str

edit make_num make

//产生虚拟变量

replace dummy_high = 1 if pr>= 10000//用1取代价格大于某数的dummy high

//另一种方法

gen indicator_hi = (pr>10000)//满足括号里面条件的为1

//展示出两个变量不一样的地方(check的方法)

edit pr dummy_high indicator_hi

edit dummy_high indicator_hi if dummy_high ~=indicator_high

sum dummy_hgh indicator_high

recode foreign (0=1) (1=2), gen (for_new)

//计算pr的四分卫区间, 25%, 50%, 75%

egen pr_pc25 = pctile(pr),p(25)

egen pr_pc50 = pctile(pr),p(50)

egen pr_pc75 = pctile(pr),p(75)//分别计算这这点的数值是多少

replace pr_4cat=1 if pr >=pr_pc25&pr

replace pr_4cat=2 if pr >=pr_pc50&pr

replace pr_4cat=3 if pr >pr_pc75

/数据合并

//数据纵向合并

sysuse auto.dta,clear

keep if foreign == 0//只保留国产的数据

se auto_domestic.dta, replace

keep if foreign == 1

se auto_foreign.dta,replace

append using auto_domestic.dta//合并

//数据横向合并

gen id = _n//给横向排序(车型号)

se auto_tech.dta, replace

gen id_=n

drop make mpg weight length//丢掉一些数据

merge

merge 1:1 id using "auto_tech.dta"

sort newid year

//从小到大排序

//有些变量有些地方是没有观测值的,叫做非平衡样本

gsort newid -year

edit newid year facilityname_origin

gsort -facilityname_origin year

order so2 co newid year//按这个顺序展示这些变量

order newid, before(co)//把某个变量提到某个变量之前

//string variable字符串变量

use"nei_sample.dta",clear

edit newid facilityname_origin year

sort newid facilityname_origin year

gen facility_name = facilityname_origin //生成一个变量

edit facility_name facilityname_origi

replace facility_name = lower(facility_name)//变量名称小写化

upper //变量名称大写花

//trim ltrim rtrim 去掉空格zuo you zhong

replace facility_name = trim(facility_name)

edit facility_name

replace facility_name = ltrim(facility_name)

replace facility_name = rtrim(facility_name)

replace facility_name = subinstr(facility_name,","," ",.)

replace facility_name = subinstr(facility_name,"."," ",.)

replace facility_name = subinstr(facility_name,"/"," ",.)

replace facility_name = subinstr(facility_name,"#"," ",.)

//替代标点,全部替代用空格替代

replace facility_name = subinstr(facility_name,":"," ",.)

replace facility_name = subinstr(facility_name,"’"," ",.)

replace facility_name = subinstr(facility_name,""," ",.)

replace facility_name = subinstr(facility_name,":"," ",.)

replace facility_name = subinword( facility_name,"company"," ",.)

replace facility_name = subinstr(facility_name,"co"," ",.)

replace facility_name = subinstr(facility_name,"inc"," ",.)

replace facility_name = subinstr(facility_name,"lp"," ",.)

replace facility_name = ltrim(facility_name)

replace facility_name = subinstr(facility_name,"u s","us",.)

gen flag = 1 if regexm(facility_name,"u s")==1

//生成新的变量 将带有u s 的变量标注为一,帮助寻找

gen flag2 = 1 if regexm(facility_name,"us")==1

split facility_name

gen fac_name = facility_name1+" "+facility name2

edit zipcode

split zipcode,parse(-)

//按照某种符号拆分字符串

edit zipcode

substr //截取

gen zip5=substr(zipcode,1,5)//生成zip5,表示截取zipcode的前五位

edit zipcode zip5 if length(zip5) ~=5 //展示长度不等于5的zip5和zipcode

edit zip5

gen len_cn = ustrlen(zipcode) //生成中文字符串长度

edit fips

gen fips2 = substr(fips, 1,2)

edit fips2

gen fips3 = substr(fips, 3,3)

edit fips2 fips3

destring fips2, replace force

destring fips3, replace force

tostring fips2 fips3, replace force

edit fips2 fips3

replace fips2="0"+fips2 if length(fips2)==1

replace fips3="0"+fips3 if length(fips3)==2

replace fips3="00"+fips3 if length(fips3)==1

//前面用零补齐,补成五位

duplicates//重复观测值

sort newid

duplicates report newid year //报告重复观测值

duplicates tag newid year, gen(dup)

tab dup//展示

edit new year if dup>=177

duplicates drop newid year,force //去掉重复样本

duplicates report newid year

ssc install unique //安装unique

unique newid year//展示有几个是的

unique fips

collapse (sum) so2 co nox nh3 voc (first)facilityname_origin fips zipcode, by(newid year)

//关于newid year重复的字符串变量,只取个,数值变量加总

collapse (sum) so2 co nox nh3 voc (count)newid , by(fips year)

//关于fips year 加总。。。 数出newid

//改变面板数据的结构

reshape

keep newid year so2

reshape wide so2 , i(newid) j(year)

reshape long so2, i(newid) j(year)

//

duplicates drop newid year, force

unique newid year

keep newid year so2 co nox voc nh3 sic

reshape wide so2 co nox voc nh3, i(newid sic) j(year)

//数据变少了是因为有的newid对应多个sic

//reshape来回两次就是平衡面板数据

//quiz reshape id- year-pollutant-emissions

keep newid year so2 co nox voc

ren (so2 co nox voc)(pol1 pol2 pol3 pol4 )

reshape long pol, i(newid year) j(type)

tostring type,replace force

replace type= "so2" if type=="1"

edit newid year so2

edit newid year so2

sort newid year

by newid: gen lag1so2=so2[_n-1]

//滞后一行,不一定滞后一期

by newid:gen f1so2=so2[_n+1]

bys newid: gen Nso2 = so2[_N]//一期

//滞后一期,解决不平衡面板

xtset newid year

gen lso2=l.so2

//

edit fips newid year

sort fips newid year

by fips year: egen id_sum=count (newid)

edit fips year newid so2

by fips year:egen so2_fips=sum(so2)

//

collapse(sum) so2 co nox nh3 voc (first) facilityname_origin fips

//加总一个地区的所有公司的污染构造这个地区的总污染量

duplicates report newid year

collapse (sum) so2 co nox nh3 voc (count) newid ,by(fips year)

//collapse by 2-digit sic and fips_state and year

gen state = substr(fips,1,2)

gen sic2 = substr(sic,1,2)

collapse (sum) so2 co nox nh3 voc ,by(state sic2 year )

AnintroductiontoR:全面系统地介绍R语言,适合作为初步的参考资料。该资料是一份pdf文档,也是R语言手册。TryR:强烈,非常简短地课程,可以在网页上进行简短的作。该网站提供R的网页作,所以你无需安装R,从最基本的R语言开始学期,通过实际作掌握R的相关知识。ComputingforDataAnalysis:大约四周的视频课程。IntroductiontoRforDataMining:R进行数据挖掘方面的材料,包括一些ppt和视频资料Rstudio:R语言的集成作环境,强烈建议安装。Rstudio会让你的工作效率指数提高。GettingstartedwithRandHadoop,关于R和Hadoop项目的资料。ggplot2:R绘图神器,该网站提供所有关于ggplot2的命令分解和介绍,同时配有大量的案例。LearningTimeSerieswithR:关于R的时间序列分析的资料。

stata中怎么将包含某两个字符的数据归位一类

encode make, gen(make_num)//将文字变量重新编码成数字

也可简label define category 0 "less than 5k" 1 "between 5k and 10k" 2 "more than 10k"写为bys,比如想以性别为类别分组做统计分析,bys 性别:后面再加统计描述的变量如sum,tab,mean,median等

//拆分数据

stata里说id是一个字符串变量怎么办

append mergegen dummy_high = 0//生成一个全是零的新变量 joinby/

可转换为数值。

//按10m的大小拆分文件

也可以这样处理,先保持这个变量为字符型的变量,然后有substr的命令,提取前四位,只要年份,然后再转化为数值型,就可以计算了。

Stata是一套提供其使用者数据分析、数据管理以及绘制专业图表的完整及整合性统计软件。该软件提供的功能包含线性混合模型、均衡重复反复及多项式普罗比模式。

在stata中如何对相同的字符型数据进行分类汇总呢?

//in:第几edit newid year so2//调用这三个变量行到第几行(L表示一行)

所以说都写了同一个户主的名字,那么我们就可以用户主那个变量当作household id. 如果你的数据是string的,你可以encode该变量,使其变成numerical的变量。这样sum一下你就知道一共有多少户人家了,要知道每一户有多少人,你可以(bysort hhid:) gen hh_no=hhid[_n] if _n>=1 (括号里可能要加可能不加,不确定)。。。你自己 _n or _N了解detail 也好。

export_delimited_options]/

STATA中如何将字符型变量转化成日期型变量,多谢

use replace pr_g = pr_g2 if foreign == 0"nei_sample.dta",clear

字符型的可电子邮件地址拆分为“@”:以转换成datetime 来用阿 select datediff(year,convert(datetime,'20070802'),convert(datetime,'2009/06/05')) 希望能帮上你 'ta290001008','ta500251056'怎么理解这2个日期? 把数据的格式写出来,就是写出一个时间的例子来 看看下面的行不? select datediff(year,convert(datetime,ta290001008),convert(datetime,ta500251056)) from 表

关于stata的数据分析为什么数据都是红色的

可能是查找字符串首次出现的位置())。

红色数据表示字符串变量,这是不能用于回归分析的.一般在做面板回归的时候,直接从excel将数据黏贴到STATA里地区变量是字符串变量,需要进行转换.但是你这里除了年份的数据是数值型的,其他的都是红色就有问题了.我的建议是:

gen pr_4cat=0

(1)在excel中详细检查每一个变量下的数据,尤其注意有没有缺漏值,很多时候存在缺漏值是导致字符串变量的重要原因.

(2)对于地区这一变量,一般是将其进行转换.假设地区变量名为region,具体的作命令是:

encode,gen(region1)egen pr_g = mean(pr) if foreign == 1 /重新生成一个带标签值的变量/

rename region1 region /将region1的名称改为region/

这时候region的颜色应该为蓝色,进行时间序列或面板数据的设定就没有问题了.

stata中为什么出现country is a string variable

inconsistent

可能是数据输入格式的问题,你可以手动输入几个数据试试,不要直接导入或者从excel上粘贴。

Stata 是一套提供其使用者数据分析、数据管理以及绘制专业图表的完整及整合性统计软件。它拥有很多功能,包含线性混合模型、均衡重复反复及多项式普罗比模式。用Stata绘制的统计图形相当精美。

Stata//重命名 是一套reshape long so2 co nox voc nh3, i(newid sic) j(year)提供其使用者数据分析、数据管理以及绘制专业图表的完整及整合性统计软件。它拥有很多功能,包含线性混合模型、均衡重复反复及多项式普罗比模式。

用Stata绘制的统计图形相当精美。

可以不学习stata,只学习r语言吗

import excel " "

/ Lecture 1: How to import and export data /

cd"D:\BaiduNetdiskDownload\stata application"

//备注不长于一行可用双斜杠

sysuse "auto.dta",clear

//导入系统自带的数据,clear:关闭之前所有的数据

//字符串变量:红颜色的文字变量。蓝颜色的domestic对应0,foreign对于1

//export data

export delimited /stata 自带的帮助文档,只导出csv和txt格式/

/导出其中部分变量

export delimited [你想导出的变量名称] using filename [if] [in] [,

export delim using"auto.txt",replace

export delim make pr using "auto.csv",replace

//MAKE 和pr是变量名,只导出这两个变量

import excel

export excel using "auto.xlsx" in 11/L

export excel using "auto.xlsx" if pr>=4000,replace

//if: 满足某种条件的变量

export excel using "auto.xlsx" firstrow(var),replace

use "D:\BaiduNetdiskDownload\stata application\Data_ma.dta"

unicode

clear

unicode encoding set gb18030

unicode translate "Data_ma.dta"

use "Data_ma.dta",clear

//打开大数据

set excelxlsxlargefile on

//允许stata打开大文件

//如果内存太小,如何拆分大文件

findit cky

cky

cky using "数据文件名称", peek(1)

//看一下数据,不导入,(1)表示行

//分析

cky using 数据文件名称 ,cksize(10m) header(include) stub(数据文件名称)replace

import delimited using "其中一个数据文件名称”,clear encoding(数据)

//调用拆分后的其中一个数据

sysuse auto.dta,clear

//调用系统数据

export delim "auto1.csv" in 1,replace

export delim "auto2.csv" in 2, replace

export delim "auto3.csv" in 3,replace

export delim "auto4.csv" in 4, replace

export delim "auto5.csv" in 5/L, replace

foreach

foreach num of numlist 1/3 5 8 9(10) 100{

display `num'

}//重新导入数据

foreach num of numlist 1/74{

display "auto `num'"

se "auto`num'.dta", replace

} append

append using "auto2.csv" "auto3.csv""auto4.csv"/

/"auto5.csv"

use "auto1.dta", clear

foreach num of numlist 2/5{

append using "auto`num'.dta"

}se"auto_new.dta", replace

ssc install openall

findit openall

openall, auto?, insheet

//删除多余文件

foreach x of numlist 1/5{

erase"auto`x'.csv"

}//lecture 3

cd "D:\BaiduNetdiskDownload\stata application"

format

sysuse auto.dta

//改变字符串的长度,为了方便浏览

format %30s make

//浏览

edit make

//显示字符串和数值变量

edit make pr headroom

format %-20s make

//小数点后面两位数

format %3.2f headroom

//

format %10.0g pr

list make pr headroom in 1/10

//变量的标签

//介绍数据集的基本情况

//改变整个数据的标签

label data "US auto data美国汽车数据"

//改变变量标签

label var pr "auto pr汽车价格"

//修改虚拟变量标签,先定义后标签

label define origin_v 0 "国产" 1 "进口"

label values foreign origin_v

replace foreign = 2 in 1/8

//每一次定义新标签要重新命名origin

label define origin_new2 0 "国产" 1 "进口" 2 "unknown"

//显示

list make if foreign == 0 //一个等号表示赋值

list make pr if make =="AMC Concord" /

/ | make =="Merc. Cougar"

list make foreign pr if (foreign == 1& pr <=5000) /

/ | (foreign == 0 & pr >3000)

list make pr if inlist (make, "AMC Concord" "Merc. cougar", )

sysuse auto.dta , clear

export excel using "auto.xlsx", nolabel replace

import excel using "auto.xlsx", clear

//修改变量名称

rename

rename A pr

rename _all, proper//让变量首字母大写,剩下字母小写

//将变量名称批量写入标签, 重要的是所有的变量循环_all

foreach v of varlist _all {

label variable `v' " `v' "

generate//建立新变量

cd "D:\BaiduNetdiskDownload\stata application"

gen pr2=pr^2 //生产pr2的平方

gen pr_mpg = prmpg if foreign == 1 //生成pr 和mpg的交叉项 如果foreign=1

replace pr_mpg =0 if pr_mpg == .

//用0取代缺失值

gen logpr = log(pr) //生产pr的对数指

gen lnpr = ln(p) //生成pr的自然对数指,其实和上行生成的结果一样的

//出现零的时候取对数会成为missing数据,可能会丢失数据

replace pr_mpg = prmpg //生成pr和mpg的交叉项,并取代pr_mpg变量

gen prcateg = 0//生产prcateg变量,并将数据分组

replace prcateg = 1 if pr >=5000 & pr < 10000

replace prcateg = 2 if pr >= 10000

label values prcateg category

egen

egen prg3 = mean(pr)

gen pr_dev = pr - prg3

//如何分别计算foreign和domestic的均值

sort foreign

//方法一

egen pr_g2 = mean(pr) if foreign == 0

drop pr_g2

//方法二

by foreign: egen prg_by = mean(pr)//按照foreign的分类做均值

sort foreign //给foreign按数据大小排序

tostring // 数值变量变字符串

destring //字符串变数值变量,字符串不可以做运算的

edit mpg

tostring mpg, gen(mpg_str)

tostring mpg, replace force//不想产生新的变量,有mpg取代原来的

edit mpg mpg_str

destring mpg_str, replace force//将字符串变数值

edit mpg mpg_str

edit make_num make

//产生虚拟变量

replace dummy_high = 1 if pr>= 10000//用1取代价格大于某数的dummy high

//另一种方法

gen indicator_hi = (pr>10000)//满足括号里面条件的为1

//展示出两个变量不一样的地方(check的方法)

edit pr dummy_high indicator_hi

edit dummy_high indicator_hi if dummy_high ~=indicator_high

sum dummy_hgh indicator_high

recode foreign (0=1) (1=2), gen (for_new)

//计算pr的四分卫区间, 25%, 50%, 75%

egen pr_pc25 = pctile(pr),p(25)

egen pr_pc50 = pctile(pr),p(50)

egen pr_pc75 = pctile(pr),p(75)//分别计算这这点的数值是多少

replace pr_4cat=1 if pr >=pr_pc25&pr

replace pr_4cat=2 if pr >=pr_pc50&pr

replace pr_4cat=3 if pr >pr_pc75

/数据合并

//数据纵向合并

sysuse auto.dta,clear

keep if foreign == 0//只保留国产的数据

se auto_domestic.dta, replace

keep if foreign == 1

se auto_foreign.dta,replace

append using auto_domestic.dta//合并

//数据横向合并

gen id = _n//给横向排序(车型号)

se auto_tech.dta, replace

gen id_=n

drop make mpg weight length//丢掉一些数据

merge

merge 1:1 id using "auto_tech.dta"

sort n//在result窗口展示数据ewid year

//从小到大排序

//有些变量有些地方是没有观测值的,叫做非平衡样本

gsort newid -year

edit newid year facilityname_origin

gsort -facilityname_origin year

order so2 co newid year//按这个顺序展示这些变量

order newid, before(co)//把某个变量提到某个变量之前

//string variable字符串变量

use"nei_sample.dta",clear

edit newid facilityname_origin year

sort newid facilityname_origin year

gen facility_name = facilityname_origin //生成一个变量

edit facility_name facilityname_origi

replace facility_name = lower(facility_name)//变量名称小写化

upper //变量名称大写花

//trim ltrim rtrim 去掉空格zuo you zhong

replace facility_name = trim(facility_name)

edit facility_name

replace facility_name = ltrim(facility_name)

replace facility_name = rtrim(facility_name)

replace facility_name = subinstr(facility_name,","," ",.)

replace facility_name = subinstr(facility_name,"."," ",.)

replace facility_name = subinstr(facility_name,"/"," ",.)

replace facility_name = subinstr(facility_name,"#"," ",.)

//替代标点,全部替代用空格替代

replace facility_name = subinstr(facility_name,":"," ",.)

replace facility_name = subinstr(facility_name,"’"," ",.)

replace facility_name = subinstr(facility_name,""," ",.)

replace facility_name = subinstr(facility_name,":"," ",.)

replace facility_name = subinword( facility_name,"company"," ",.)

replace facility_name = subinstr(facility_name,"co"," ",.)

replace facility_name = subinstr(facility_name,"inc"," ",.)

replace facility_name = subinstr(facility_name,"lp"," ",.)

replace facility_name = ltrim(facility_name)

replace facility_name = subinstr(facility_name,"u s","us",.)

gen flag = 1 if regexm(facility_name,"u s")==1

//生成新的变量 将带有u s 的变量标注为一,帮助寻找

gen flag2 = 1 if regexm(facility_name,"us")==1

split facility_name

gen fac_name = facility_name1+" "+facility name2

edit zipcode

split zipcode,parse(-)

//按照某种符号拆分字符串

edit zipcode

substr //截取

gen zip5=substr(zipcode,1,5)//生成zip5,表示截取zipcode的前五位

edit zipcode zip5 if length(zip5) ~=5 //展示长度不等于5的zip5和zipcode

edit zip5

gen len_cn = ustrlen(zipcode) //生成中文字符串长度

edit fips

gen fips2 = substr(fips, 1,2)

edit fips2

gen fips3 = substr(fips, 3,3)

edit fips2 fips3

destring fips2, replace force

destring fips3, replace force

tostring fips2 fips3, replace force

edit fips2 fips3

replace fips2="0"+fips2 if length(fips2)==1

replace fips3="0"+fips3 if length(fips3)==2

replace fips3="00"+fips3 if length(fips3)==1

//前面用零补齐,补成五位

duplicates//重复观测值

sort newid

duplicates report newid year //报告重复观测值

duplicates tag newid year, gen(dup)

tab dup//展示

edit new year if dup>=177

duplicates drop newid year,force //去掉重复样本

duplicates report newid year

ssc install unique //安装unique

unique newid year//展示有几个是的

unique fips

collapse (sum) so2 co nox nh3 voc (first)facilityname_origin fips zipcode, by(newid year)

//关于newid year重复的字符串变量,只取个,数值变量加总

collapse (sum) so2 co nox nh3 voc (count)newid , by(fips year)

//关于fips year 加总。。。 数出newid

//改变面板数据的结构

reshape

keep newid year so2

reshape wide so2 , i(newid) j(year)

reshape long so2, i(newid) j(year)

//

duplicates drop newid year, force

unique newid year

keep newid year so2 co nox voc nh3 sic

reshape wide so2 co nox voc nh3, i(newid sic) j(year)

//数据变少了是因为有的newid对应多个sic

//reshape来回两次就是平衡面板数据

//quiz reshape id- year-pollutant-emissions

keep newid year so2 co nox voc

ren (so2 co nox voc)(pol1 pol2 pol3 pol4 )

reshape long pol, i(newid year) j(type)

tostring type,replace force

replace type= "so2" if type=="1"

edit newid year so2

edit newid year so2

sort newid year

by newid: gen lag1so2=so2[_n-1]

//滞后一行,不一定滞后一期

by newid:gen f1so2=so2[_n+1]

bys newid: gen Nso2 = so2[_N]//一期

//滞后一期,解决不平衡面板

xtset newid year

gen lso2=l.so2

//

edit fips newid year

sort fips newid year

by fips year: egen id_sum=count (newid)

edit fips year newid so2

by fips year:egen so2_fips=sum(so2)

//

collapse(sum) so2 co nox nh3 voc (first) facilityname_origin fips

//加总一个地区的所有公司的污染构造这个地区的总污染量

duplicates report newid year

collapse (sum) so2 co nox nh3 voc (count) newid ,by(fips year)

//collapse by 2-digit sic and fips_state and year

gen state = substr(fips,1,2)

gen sic2 = substr(sic,1,2)

collapse (sum) so2 co nox nh3 voc ,by(state sic2 year )

AnintroductiontoR:全面系统地介绍R语言,适合作为初步的参考资料。该资料是一份pdf文档,也是R语言手册。TryR:强烈,非常简短地课程,可以在网页上进行简短的作。该网站提供R的网页作,所以你无需安装R,从最基本的R语言开始学期,通过实际作掌握R的相关知识。ComputingforDataAnalysis:大约四周的视频课程。IntroductiontoRforDataMining:R进行数据挖掘方面的材料,包括一些ppt和视频资料Rstudio:R语言的集成作环境,强烈建议安装。Rstudio会让你的工作效率指数提高。GettingstartedwithRandHadoop,关于R和Hadoop项目的资料。ggplot2:R绘图神器,该网站提供所有关于ggplot2的命令分解和介绍,同时配有大量的案例。LearningTimeSerieswithR:关于R的时间序列分析的资料。

stata里说id是一个字符串变量怎么办

import delim "auto`num'.csv", c可以使用bysort命令。lear

可转换为数值。

也可以这样处理,先保持这个变量为字符型的变量,然后有substr的命令,提取前四位,只要年份,然后再转化为数值型,就可以计算了。

Stata是一套提供其使用者数据分析、数据管describe理以及绘制专业图表的完整及整合性统计软件。该软件提供的功能包含线性混合模型、均衡重复反复及多项式普罗比模式。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 12345678@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息