how can get the first and last observations in a dataset
using Proc SQl?

Answer Posted / rohitbedi

Create a new data set as below:

data demo;
input year month cpi;
datalines;
1990 6 129.9
1990 7 130.4
1990 8 131.6
1990 9 132.7
1991 4 135.2
1991 5 135.6
1991 6 136.0
1991 7 136.2
;
run;

Sort the data if needed.

Create a new id field:

data demo2;
set demo;
id = _n_;
run;

Display the first and last record for every date and finally remove the id column.

proc sql;
create table final as select * from demo2 where id in (select min(id) from demo2 group by year) or id in (select max(id) from demo2 group by year);
alter table final drop column id;
quit;

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

describe about metadata object? : Sas-di

615


what is program data vector? : Sas-administrator

606


explain what is factor analysis? : Sas-administrator

592


how do you want missing values handled? : Sas programming

528


What are the five ways to do a table lookup in sas? : sas-grid-administration

584






what type of graphs we will create(for 2+years candidates)?

1896


List down the reasons for choosing sas over other data analytics tools.

798


how can you import .csv file in to sas? : Sas programming

627


Do you need to compute new variables? If so,should you do this before you execute the report-writing procedure?

1460


Intern stastical programmer written test

276


Have you ever used the SAS Debugger?

1201


What do you code to create a macro? : sas-macro

563


What are the implications?

1175


what has been your most common programming mistake? : Sas programming

551


What is the good sas programming practices for processing large data sets?

621