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

How can you create a macro variable with in data step? : sas-macro

578


How to test the debugging in sas?

610


What would be the value of month at the end of data step execution and how many observations would be there?

583


what is information maps?

1618


what is the use of proc sql? : Sas programming

595






In ARRAY processing, what does the DIM function do?

713


How substr function works in sas?

703


name several ways to achieve efficiency in your program? : Sas programming

572


What areas of SAS are you most interested in?

1065


Given an unsorted data set, how to read the last observation to a new data set?

840


Intern stastical programmer written test

288


For what purpose would you use the RETAIN statement?

1047


How to create a permanent sas data set?

678


What does PROC print, and PROC contents do?

730


What do you know about sas and what we do? : sas-grid-administration

606