Help using Base SAS procedures

NEED HELP PLEASE. DATA SELECTION

Reply
Contributor
Posts: 56

NEED HELP PLEASE. DATA SELECTION

Really need help for the problem as below.
This is the pseudo data table.
For three stocks, I wanna extract the data between 1 and 2. Dots are the missing values.
Note that For Stock B, 2 does not appear on the table. but we still need to select those between date 2.4 and 2.7.

I am looking for a generalized codes for performing the selection, as my real data consists of hundreds of stocks.

Thank you so much first !!!

DATE STOCK PRICE Volume first/last
1.1 A . .
1.2 A . .
1.3 A . .
1.4 A 0.5 200 1
1.5 A 0.6 1000
1.6 A . .
1.7 A 0.5 300 2
1.8 A . .
1,9 A . .
2.1 B . .
2.2 B . .
2.3 B . .
2.4 B 0.75 400 1
2.5 B 0.6 2000
2.6 B . .
2.7 B 1..5 300
3.1 C . .
3.2 C . .
3.3 C 2 5000 1
3.4 C 1.5 2000
3.5 C 2 1000
3.6 C 1.75 3000 2
3.7 C 1.8 5000
Super Contributor
Super Contributor
Posts: 3,174

Re: NEED HELP PLEASE. DATA SELECTION

Posted in reply to Fred_Gavin
Use SAS PROC SUMMARY with MAX and MIN to get your CLASS variable value ranges for selection, creating a separate SAS file. Then using a DATA step and your data file sorted appropriately, MERGE (with a BY on your CLASS variables) back in the MIN/MAX range file and use IF THEN OUTPUT; to subset your data file.

Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 174

Re: NEED HELP PLEASE. DATA SELECTION

Posted in reply to Fred_Gavin
I think this will do the job... you will need to change how date is read etc, but you have something to work with (if I understand your question correct)

[pre]data input;
infile datalines missover dlm=" ";
input DATE :$3. STOCK $ PRICE Volume first_last;
datalines;
1.1 A . .
1.2 A . .
1.3 A . .
1.4 A 0.5 200 1
1.5 A 0.6 1000
1.6 A . .
1.7 A 0.5 300 2
1.8 A . .
1.9 A . .
2.1 B . .
2.2 B . .
2.3 B . .
2.4 B 0.75 400 1
2.5 B 0.6 2000
2.6 B . .
2.7 B 1.5 300
3.1 C . .
3.2 C . .
3.3 C 2 5000 1
3.4 C 1.5 2000
3.5 C 2 1000
3.6 C 1.75 3000 2
3.7 C 1.8 5000
;run;

data result(drop=flag);
retain flag 0;
set input(sortedby=stock);
by stock;
if first.stock then flag=0;
if first_last = 1 then flag=1;
if flag = 1 then output;
if first_last = 2 then flag=0;
run;[/pre]
Ask a Question
Discussion stats
  • 2 replies
  • 114 views
  • 0 likes
  • 3 in conversation