BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jerrylshen
Obsidian | Level 7

Given an input table, how to only select the lower RecordNumber of the same subject?

 

Sample Input Data:

╔═════════╦═══════╦═════════╗
║ Subject          ║ Time1       ║ Record          ║
╠═════════╬═══════╬═════════╣
║ 1                    ║ 11:13        ║ 1                    ║

║ 1                    ║ 11:13        ║ 2                    ║

║ 2                    ║ 11:17        ║ 5                    ║

║ 2                    ║ 11:17        ║ 6                    ║
╚═════════╩═══════╩═════════╝

 

Output Data:

╔═════════╦═══════╦═════════╗
║ Subject          ║ Time1       ║ Record          ║
╠═════════╬═══════╬═════════╣
║ 1                    ║ 11:13        ║ 1                    ║
║ 2                    ║ 11:17        ║ 5                    ║
╚═════════╩═══════╩═════════╝

 

For Subject 1, Record 1 < Record 2. For Subject 2, Record 5 < Record 6

 

I tried doing something like this to first get the lowest RecordNumber and then trying to filter out the input data, but it still outputs essentially everything, unless I did something wrong here.

CREATE TABLE recordtemp AS
SELECT DISTINCT Subject, min(RecordPosition) as RecordPosition,
FROM output1
;

m open to either proc sql/SAS advices, but I am more experienced in SQL but I can't think of a way for this.   

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
/**** UNTESTED CODE *****/

proc sql;
    create table recordtemp as select * from output1
        group by subject having record=min(record);
quit;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
/**** UNTESTED CODE *****/

proc sql;
    create table recordtemp as select * from output1
        group by subject having record=min(record);
quit;
--
Paige Miller
jerrylshen
Obsidian | Level 7

Accepting this answer, but I actually found this post https://communities.sas.com/t5/SAS-Programming/Proc-SQL-query-Select-Minimum-Record/td-p/296572 like right before you replied lol but thanks for the quick reply!

millerm
Fluorite | Level 6

If you're using proc sql Paige's is the best way to go, but I normally find with larger datasets a proc summary works a bit better, and is irrespective of other variables, if that's of use.

proc summary data=your_data;
by descending subject time1;
var record;
output out=your_output_dataset min=;
run;

That should work!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1121 views
  • 0 likes
  • 3 in conversation