【SAS】高级SAS面试问答.pdfVIP

  • 48
  • 0
  • 约1.05万字
  • 约 15页
  • 2020-02-14 发布于广东
  • 举报
1. Two ways to select every second row in a data set data example; set sashelp.class; if mod(_n_,2) eq 0; run; MOD Function returns the remainder from the division of the first argument by the second argument. _N_ corresponds to each row. The second row would be calculated like (2/2) which returns zero remainder. data example1; do i = 2 to nobs by 2; set sashelp.class point=i nobs=nobs; output; end; stop; run; 2. How to select every second row of a group Suppose we have a table sashelp.class. We want every second row by variable sex. proc sort data = sashelp.class; by sex; run; data example2 (drop = N); set sashelp.class; by sex; if first.sex then N = 1; else N +1; if N = 2 then output; run; 3. How to calculate cumulative sum by group Create Sample Data data abcd; input x y; cards; 1 25 1 28 1 27 2 23 2 35 2 34 3 25 3 29 ; run; Cumulative Sum by Group Cumulative Sum by X data example3; set abcd; if first.x then z1 = y; else z1 + y; by x; run; 4. Can both WHERE and IF statements be used for subsetting on a newly derived variable? SAS : WHERE vs. IF No. Only IF statement can be used for subsetting when it is based on a newly derived variable. WHERE statement would return an error newly derived variable is not on file. Please note that WHERE Option can be used for subsetting on a newly created variable. data example4 (where =(z =50)); set abcd; z = x*y; run; 5. Select the Second Highest Score with PROC SQL data example5; input Name $ Score; cards; sam 75 dave 84 sachin 92 ram 91 ; run; proc sql; select * from example5 where score in (select max(score) from example5 where score not in (select max(score) from example5)); quit; 6. Two ways to create a macro variable that counts the number of observations in a dataset data _NULL_; if 0 then set sashelp.class nobs=n;

文档评论(0)

1亿VIP精品文档

相关文档