Pick only first 3 females from the dataset

r
sas
data_wrangling

#1

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

Regards,


#2

I think this can do…

female=grep(“F”,data$Sex,fixed=TRUE)
newdata<-data[female[1:3],]


#3

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


#4

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.


#5

is this GREP function available in v9.1 ?


#6

That code is for R not SAS !


#7

@Armaan

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;
Cnt+1;
End;

run;

Imran


#8

then its very surprising …