Pick only first 3 females from the dataset



pick only first 3 females from the dataset and the order of any obs should not get disturbed in the resultant dataset.
only smart coding.

not with firstobs and obs



I think this can do…



can u be more specific to the above code…i didn’t get that.
is GREP a function ?


Yes GREP is a function,

Assuming initial data set is named as data

female=grep(“F”,data$Sex,fixed=TRUE) This grep function searches for character “F” in Sex column of the given data and returns the indices or row numbers.

So, vector female is generated which is the row numbers of all the females.

here the female vector will have 3 5 6 8 10 …

As we have the row numbers, and the question is to extract first 3 females to new dataset,

newdata<-data[female[1:3],] This creates new dataset taking rows 3,5,6[first 3 females] of original data.


is this GREP function available in v9.1 ?


That code is for R not SAS !



You can go with below code to solve this. Below I have used conditional statement with counter increment of female count.

Data Rest;
Set Src_Data;

if Sex="F" and Cnt lt 3 then do;
output Rest;




then its very surprising …