1、定義變量的長(zhǎng)度——length
建立SAS數(shù)據(jù)集時(shí),定義sas數(shù)據(jù)集中每個(gè)變量的長(zhǎng)度日裙〈低В可以將定義長(zhǎng)度的句子寫在data和set之間,要不然很可能導(dǎo)致在提醒中顯示數(shù)據(jù)集已經(jīng)定義了變量的長(zhǎng)度昂拂。
舉例:將education數(shù)據(jù)集中edu變量的長(zhǎng)度定義為200個(gè)字符受神。
data education;
length edu $200;
set education;
run;
2、選擇特定行數(shù)或者范圍內(nèi)的觀測(cè)(文件的第N行_N_和rom_num):
1)取文件的第一行:
if _N_=1;
2)生成行號(hào)的變量rom_num格侯,則取文件的第一行也可以用:rom_num=1;
data education;
rom_num+1;
set education;
run;
3鼻听、定義變量標(biāo)簽,使proc freq步中得到的結(jié)果能顯示變量對(duì)應(yīng)的標(biāo)簽联四。
data dataw.work_code;
set dataw.work;
/*加標(biāo)簽的方式1:用label語(yǔ)句*/
label code_group = 'code_group'
? ? ? ? ? ? 1 = '101'
? ? ? ? ? ? 2 = '102'
? ? ? ? ? ? 3 = '103'
? ? ? ? ? ? 4 = ‘104'
? ? ? ? ? ? 5 = '105'
? ? ? ? ? ? 6= 'others';
if code=11111 then code_group=1;
else if code=22222 then code_group=2;
else if code=33333 then code_group=3;
else if code=44444 then code_group=4;
else if code=55555 then code_group=5;
else code_group=6;
run;
proc sort;by code_group;run;
/*加標(biāo)簽的方式2:proc format撑碴,因?yàn)閏ode_group 變量本身是數(shù)值型變量,所以用如下的寫法*/
proc format;
value code_group 1 = '101'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2 = '102'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3 = '103'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4 = '104'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5 = '105'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 6 = 'others';
run;
/*如果code_group是字符型變量朝墩,則采用如下的寫法
proc format;
value $code_group '1' = '101'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '2' = '102'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '3' = '103'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '4' = '104'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '5' = '105'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '6' = 'others';
run;*/
proc freq data=dataw.work_code;
tables code_group;
format code_group code_group.;
run;
4醉拓、對(duì)數(shù)據(jù)集進(jìn)行批量改值
1)對(duì)數(shù)據(jù)集中的字符型字段進(jìn)行改值
data collect;
set collect;
array char _char_;
do over char;
if strip(char)="-8"|strip(char)="-7"|strip(char)="-2"|strip(char)="-1" then char ="";
end;
run;
2)對(duì)數(shù)據(jù)集中的數(shù)值型字段進(jìn)行改值
data collect;
set collect;
array num _numeric_;
do over num;
if num in ("-8" "-7" "-2" "-1") then num =.;
end;
run;
至此,我們對(duì)數(shù)據(jù)集中所有文本型收苏、數(shù)值型的-8亿卤、-7、-2鹿霸、-1分別填充為了空白和缺失排吴。