1. sas如何获取某个变量的第一个和最后一个值
proc sql;
select name into:first_name from (select monotonic() as num,* from sashelp.class) having num=min(num);
select name into:last_name from (select monotonic() as num,* from sashelp.class) having num=max(num);
quit;
or data step
data _null_;
set sashelp.class end=last;
if _n_=1 then call symputx('first_name',name);
if last then call symputx('last_name',name);
run;
%put& first_name& last_name;
2. SAS如何提取出最后一个字符相同的所有变量
data one;
input az bz cz at bt ct xu hu;
cards;
1 2 3 4 5 6 7 8
;;;
run;
data three;
set one;
id = open('work.one');
length last $1;
do i =1 to 8;
get=varname(id,i);
last=substr(get,length(get),1);
if last="z" then var_with_z=get;
else if last="t" then var_with_t=get;
else var_unkown=get;
output;
call missing (var_with_z,var_with_t,var_unkown);
end;
drop i id get ;
run;
先用open function打开你的原数据集 并将这个数据集的id的值赋予一个变量id
varname function结合代表数据集的id和变量所在位置的数字 可以知道每个变量的名字
call missing是为了防止 几个变量retain他们的值
3. 在SAS程序中,我想在一段中文字段中提取最后五个字,怎么做?
使用Substr(s,p,n)函数,提取字符:Substr(s,p,n)从字符串s中的第p个字符开始提取n个字符的子串。
4. 您说SAS中用end=last,然后用if last来判断是否为最后一个观测值,那如何判断是否为第一个观测值呢?
额,这个还是我写的,那我也来答答吧,
sas 里面有几个系统变量,比方说
_N_ _ERROR_ FIRST. LAST. END 等等
但是是没有begin(或者start)的比方说如果你要取第一条你可以这样写
Data A;
Set Sashelp.Class End=Last;
Tot+Age;
If _N_>1 Then stop;
Run;
此外,end 是作为当前是否到达文件底部的标志,也就是说只有当前读入的记录是最后一条的时候end 对应的值为1其余都是为0的。
5. 在sas中我想要使用数据集的某一行数字或者某一列数字怎么弄?
使用某一列很好办。 某一列其实就是一个变量,如果你只想保留这个一个变量的话:
Data new;
set old(keep=a);
run;
至于如何使用某一行么,当时我也遇到过这个问题,我用了比较笨的办法,希望能够帮助到你。其实就是想办法将数据进行转置。这样使用某一行就变成了上述查询某一列的问题了。
proc transpose data=old out=new;
run;
当然直接保留某一行也是可以的
比如你只想选取第n个观察值;
Data new;
set old;
if _N_=n;
run;
PS:上述代码中 new 为输出的数据集,old 为原始数据集, a 为你所需要的某一列的变量名称
6. SAS中用substr提取字符串,我想提取前5个字符,为什么结果只显示了第一个字符?
data _null_;
a="dfadiuytfasdfasdfwewe";
b=substr(a,1,5);
put a b;
run;
结果:
dfadiuytfasdfasdfwewe dfadi
注意:substr(x,i,j)其中参数i,j是起止位置。
7. SAS中 数据按两个变量排序 然后提取出第一个变量所对应第二个变量的最小值! 比如说我有十种车,每
我这里举个例子:按照两个变量排序,然后利用first.函数
data test;
input cars $ price;
datalines;
bmw 30
das 20
bmw 27
das 22
bmw 25
das 18
;
run;
proc sort data=test;
by cars price ;
run;
data need ;
set test;
by cars price;
if first.cars then output;
run;
8. sas中如何选出需要的时间段的数据
方法如下:
data dst.ifa;
infile 'C:\Documents and Settings\CY\桌面\a.txt' dlm=',';
input date mmddyy10. time $ open high low close volume amount;
format date mmddyy10.;
run;
data dst.ifa;
set dst.ifa(where=(date='04/MAY/2010'd));
run;
在第一个数据步data dst.ifa;中,定义的数据集名为dst.ifa,在set语句中,缺省的引用数据集为work.ifa.而且语句date='05/04/2010'非法,必须将日期转换成SAS的日期格式,即data='04/MAY/2011'd;.