SAS advanced lab 机经汇总

在一个program用预存的%scoreit(your-saved-program)运行,log里面反馈一个response。

一、proc sql

1、在proc sql里创建一个table,table里包含表1中MAKE和TYPE的唯一值和表二中的rebate。
Make、Type、Rebate

proc sql;
    create table cert.car as 
        select cars.make,cars.type,rebate.rebate
        from cert.cars full join cert.rebate 
        on car.make=rebate.make
        and
        on cars.type=rebate.type;
quit;

2、在sql中创建新变量

libname cert "c:/cert/input";
proc sql;
    create table result as
        select month,data,
               case
                   when var1<10 then "low"
                   when var1=10 then "mid"
                   else "high"
               end as var_new
    from cert.input04;
quit;

3、用proc sql创建一个table,按组求平均值大于50000的内容。

proc sql;
    select avg(x) 
	from data1
	group by var2
    having avg(x)>50000;
quit;

二、array

1、写一个array LS,有LENGTH1-LENGTH3,另外一个array NEW,有NEWLENGTH1-NEWLENGTH3,有一个data set,LS单位是in,NEW是换算后的cm,NEW(i)=2.54*LS(i)

data act01;
    set cert.input02;
    array LS(*) LENGTH1-LENGTH3;
    array NEW(*) NEWLENGTH1-NEWLENGTH3;
    do i=1 to 3;
        NEW(i)=2.54*LS(i);
    end;
    drop i;
run;

2、dataset中的数值变量中missingvalue变成0。

libname cert "c:/cert/input";
data miss;
    set cert.input02;
    array vars(*) _numeric_;
    do i=1 to dim(vars);
        if vars(i)=. then vars(i)=0;
    end;
    drop i;
run;

3、array把q1-q10的A,B,C,D,E换成1,2,3,4,5存在num1-num10中

proc import datafile="/folders/myfolders/certadv/ABCDE.xlsx"
	dbms=xlsx
	out=abcde
	replace;
run;
data result;
	set abcde;
	dorp i;
	array var_q(10) q1-q10;
	array var_num(10) num1-mun10;
	do i=1 to 10;
		if(var_q(i)=A) then var_num(i)=1;
		else if(var_q(i)=B) then var_num(i)=2;
		else if(var_q(i)=C) then var_num(i)=3;
		else if(var_q(i)=D) then var_num(i)=4;
		else if(var_q(i)=E) then var_num(i)=5;
	end;
run;

4、把tax1-tax15演算成tot1-tot15,tot1=tax1*10;

data miss;
	set cert.input004;
	array var_tax(15) tax1-tax15;
    array var_tot(15) 
	do i=1 to 15;
		var_tot(i)=var_tax(i)*10;
	end;
	drop i;
run;

 

 三:macro

1、check题:直接%check()就行了,添加mprint,mlogic看log即可。

2、写一个含有comment的macro(comment的内容不能执行),后面macro的内容是一个proc print 的语句,最后执行这个macro。

3、定义一个global variable X,initial value is 1.25,用do循环 increment 0.25,一直到2。
整数运算:%eval()
浮点运算:%sysevalf()

%let X 1.25;
%macro loop(Y);
	%do %until (&Y>2)
		%put &Y;
		%let Y=%syseval(&Y+0.25);
	%end;
%mend loop;
options mprint mlogic;
%loop;

4、建一个macro,在do loop里面写一个if语句

%macro loop(start,end);
    %do i=&start %to &end;
        %if &i>10 %then %do;
            data x&i;
                x=&i;
            run;
        %end;
    %end;
%mend;
%loop(1,200);

5、用sql把region=‘AMR’的avg(var)存成一个macro variable 用into:mvar

proc sql;
    select agv(revcargo) into:mvar
        from certadv.cargorev
        where region='AMR';
quit;

 四:proc fcmp

1、in-cm的转换

libname cert "c:/cert/input";
proc fcmp outlib=work.functions.dev;
	function change(in);
        cm=2.45*in;
		return(cm);
	endsub;
quit;
options cmplib=work.functions;
data length;
	set all;
	height=change(fee);
run;

五:hash

data info19 error19l;
    drop rc;
    length country_name $30;
    if _n_=1 then do;
	    call missing(country_name);
	    declare hash C(dataset:'country19');
	    c.definekey('country_code');
	    c.definedata('country_name');
	    c.definedone();
    end;
    set countre20;
    rc=c.find;
    if rc=0 then OUTPUT info19;
    else output error19;
run;

 

你可能感兴趣的:(不熟悉的知识点)