SAS Base基本操作

SAS Base

数据基本操作 filename、infile/input、libname、proc contents

filename

​ 作用:将文件设置为一个变量,用于读取数据。

filename demo 'F:\User\classdata\demo.txt';
/* 将demo.txt文件复制给demo */

infile/input

​ 作用:用于数据的读取和设置变量。

filename demo 'F:\SU_Files\classdata\demo.txt';
/* 将demo.txt文件复制给demo */
data demo;
	infile demo;
	input id day month $ year @35 date date10.@54 age sex $; 
	format date datetime.;
run;	

libname

​ 作用:设置库的路径。

libname classdata 'F:\SU_Files\classdata';
/* 设置classdata库路径 */

proc contents

​ 作用:输出数据集的属性信息。

proc contents data=sashelp.bmt;
run;

firstobs, obs,_n_

​ 作用:设置观测值的起始值、结束值、行数。

data bmt;
	set sashelp.bmt (firstobs=2 obs=5);
	if _n_=3 then put 'this is third line';
run;	
/*读取第2-5行数据,在读取到第3行时输出字符串'this is third line' */

where

​ 作用:根据变量设置条件。

data cars;
	set sashelp.cars ;
	where Cylinders<3;
run;	
/*读取Cylinders<3的数据 */

keep

​ 作用:保留变量。

data cars(keep= make model type );
	set sashelp.cars ;
run;	
/*保留变量make model type */

drop、output、length

​ 作用:drop删除变量;output输出行数;length定义变量长度。

data demo(drop=i ip);
	length id $10;
	do i=1 to 10;
		id=ranuni(1)*100;
		ip=ranuni(2)*100;
		output;
	end;
run;  
/*删除关键字i和ip,定义id的格式为字符型长度为10,在循环中每循环一次输出数据*/

min, max, mod, ceil, sum, int, floor, round, abs

​ 作用:最小值、最大值、取余、(向上)取整、求和、取整、(向下)取整、近似值、绝对值。

data demo;
	c1=min(5,2,3);
	c2=max(3,2,5);
	c3=mod(10,3);
	c4=ceil(5.4);
	c5=sum(4,6);
	c6=int(5.4);
	c7=floor(5.4);
	c8=round(5.4321,0.01);
	c9=abs(-2);
	put c1= c2= c3= c4= c5= c6= c7= c8= c9=;
run;	
/* c1=2 c2=5 c3=1 c4=6 c5=10 c6=5 c7=5 c8=5.43 c9=2 */

datepart,MDY, month, day, intck, intnx

​ 作用:返回时间日期中的日期、返回自定义日期,返回月、日、计算两日期之间(年月日)距离,计算距离某日期的n年月日的日期值。

data demo;
	dt='05May21 12:12:12'dt;
	dt0='05May2022'd;
	d1=datepart(dt);
	d2=month(dt0);
	d3=day(dt0);
	d4=intck("month",datepart(dt),dt0);
	d5=intnx('month',datepart(dt),5,'e');
	d6=MDY(2,2,2021);
	format d1 date10.  d5 date10. d6 date10.;
	put d1= d2= d3= d4= d5= d6=;
run;
*d1=05MAY2021 d2=5 d3=5 d4=12 d5=31OCT2021 d6=02FEB2021

left, right, trim, compress, strip, substr, scan, index, upcase, lowcase, cat(t, s, x),cats(t, s, x),catx(t, s, x)

​ 作用:去掉左侧空格、去掉右侧空格、去掉右侧空格、去掉所有空格、去掉两端空格、截取字符串、根据字符串索引获得字符串、根据字符获得字符索引、小写转大写、大写转小写、连接字符tsx,去掉tsx空格并连接、去掉sx首尾空格并连接字符t。

data demo;
	c=' s a a ';
	c1='a' || left(' s a a ') || 'z';
	c2='a' || right(' s a a ') || 'z';
	c3='a' || trim(' s a a ') || 'z';
	c4='a' || compress(' s a a ') || 'z';
	c5='a' || strip(' s a a ') || 'z';
	c6=substr(' s a a ',2,1);
	c7=scan(' s a a ',1);
	c8=index('bcdee','d');
	c9=upcase('sas');
	c10=lowcase('SAS');
	c11=cat('s','a','s');
	c12=catx('a','s','c');
	c13=cats(' a ',' s ',' c d');
	put c1= c2= c3= c4= c5= c6= c7= c8= c9= c10= c11= c12= c12=;
run;	
*c1=as a a  z c2=a  s a az c3=a s a az c4=asaaz c5=as a az c6=s c7=s c8=3 c9=SAS c10=sas c11=sas c12=sac c13=asc d

length、lengthn、lengthc

​ 作用:计数字符串长度。length计数字符(除末尾空格),无字符返回1;length计数字符(除末尾空格),无字符返回0;计数所有字符数量。

data demo;
	c=' s a  s ';
	c1=length(c);
	c2=lengthn(c);
	c3=lengthc(c);
	put c1= c2= c3=;
run;	
* c1=7 c2=7 c3=8

find、put/input

作用:find查找字符串,返回索引,参数’i’表示不区分大小写。put/input设置读取/输出时的数据格式。

data demo;
	c='made in CHINA';
	c1=find(c,'N',1);
	c2=find(c,'N','i',1);
	put c1= c2=;
run;	
* c1=12 c2=7;

data demo;
	c='123.45';
	c0=123.45;
	c1=input(c,$5.);
	c2=input(c0,$5.);
	c3=put(c,$5.);
	c4=put(c0,$5.);
	put c1= c2= c3= c4=;
run;	
* c1=123.4 c2=. c3=123.4 c4=123;

date format

date8.		01JAN12
date9.		01JAN2012
yymmdd8.	yymmdd
yymmdd10.	yyyymmdd
time5.		hh:mm 时间早于10:00则h:mm 
time7.		hh:mm:ss 时间早于10:00则h:mm:ss 
tod5.		hh:mm
tod7.		hh:mm 时间早于10:00则h:mm:ss 
tod8.		hh:mm:ss
datetime20.	01JAN2012:00:00:00
datetime18. datetime16.	  01JAN12:00:00:00

proc format

proc format;
	value range 0-60='低'
				61-80='中'
				81-100='高';
run;
data demo;
	input age @@;
	format age range.;
	cards;
	20 70 90
	;
run;	

label/rename

data demo;
	input age sex $ @@;
	cards;
	20 F 70  M 90 F
	;
run;	
data demo1(rename=(age=agg));
	set demo;
   label age = '年龄'
         sex  = '性别';
run;

proc import/proc export

proc import out=demo
			datafile=".xls"
			DBMS= xls repalce;
			getnames=yes;
			sheet= sheetname;
run;

proc export data=demo
			outfile='.xlsx'
			DBMS=xlsx replace;
			sheet=sheetname;	* 可选参数
run;			

symbol operate: =, <, >, ne、if-then

data bmt;
	set sashelp.bmt (firstobs=2 obs=5);
	if _n_=3 then put 'this is third line';
run;	
/*读取第2-5行数据,在读取到第3行时输出字符串'this is third line' */
/*
= eq	等于
^= ne	不等于
> gt	大于
< lt	小于
>= ge 	大于等于
<= le 	小于等于
** 		乘方
*/

do…end、 do until、do while

data demo;
	do i=1 to 10;
		num=ranuni(1)*100;
		output;
	end;
run;

data demo;
	i=1;
	do until (i>10);
		num=ranuni(1)*100;
		i=i+1;
		output;
	end;
run;

data demo;
	i=1;
	do while (i<10);
		num=ranuni(1)*100;
		i=i+1;
		output;
	end;
run;

do…end、 do until、do while

data demo;
	do i=1 to 10;
		num=ranuni(1)*100;
		output;
	end;
run;

data demo;
	i=1;
	do until (i>10);
		num=ranuni(1)*100;
		i=i+1;
		output;
	end;
run;

data demo;
	i=1;
	do while (i<10);
		num=ranuni(1)*100;
		i=i+1;
		output;
	end;
run;
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值