DATA Step, Macro, Functions and more

How to delete specific group of observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

How to delete specific group of observations

[ Edited ]

input dataset as below:

data ds;
input idno 1-4 visit $ 6-11 lab $17-20 value;
cards;
Su01 visit1 red1 .
Su01 visit1 red2 .
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red1 200
Su01 visit1 red2 10
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red5 .
Su01 visit1 red6 .
Su01 visit1 red7 150
Su02 visit1 red1 300
Su02 visit1 red2 45
Su02 visit1 red3 .
Su02 visit1 red4 30
Su02 visit1 red1 200
Su02 visit1 red2 10
Su02 visit1 red3 .
Su02 visit1 red4 .
Su02 visit1 red5 150
Su02 visit1 red6 250

Su03 visit1 red1 .
Su03 visit1 red2 .
Su03 visit1 red3 .
Su03 visit1 red4 .
;
run;

 

 

output should be as below:

 

idno   visit   lab    value

Su01 visit1 red1 200
Su01 visit1 red2 10
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red5 .
Su01 visit1 red6 .
Su01 visit1 red7 150
Su02 visit1 red1 300
Su02 visit1 red2 45
Su02 visit1 red3 .
Su02 visit1 red4 30
Su02 visit1 red1 200
Su02 visit1 red2 10
Su02 visit1 red3 .
Su02 visit1 red4 .
Su02 visit1 red5 150
Su02 visit1 red6 250

 

 

In value variable if the data is . from red1 to redn(lab variable) then I don't want that observations in the output.

if the variable value has . and any value(alphabets,dates,numerics)even at once,then I want the output for lab variable for red1 to redn observations for that specific occurrence (red1 to redn) only. Can you kindly let me know how to get this kind of output for huge data with unknown observations.Thank you in advance.


Accepted Solutions
Solution
‎02-23-2018 03:53 AM
Super User
Posts: 10,860

Re: How to delete specific group of observations

If I understood what you mean.

 

data ds;
input idno $ visit $  lab $value;
cards;
Su01 visit1 red1 .
Su01 visit1 red2 .
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red1 200
Su01 visit1 red2 10
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red5 .
Su01 visit1 red6 .
Su01 visit1 red7 150
Su02 visit1 red1 300
Su02 visit1 red2 45
Su02 visit1 red3 .
Su02 visit1 red4 30
Su02 visit1 red1 200
Su02 visit1 red2 10
Su02 visit1 red3 .
Su02 visit1 red4 .
Su02 visit1 red5 150
Su02 visit1 red6 250
Su03 visit1 red1 .
Su03 visit1 red2 .
Su03 visit1 red3 .
Su03 visit1 red4 .
;
run;
data have;
 set ds;
 by idno;
 if first.idno or lab='red1' then group+1;
run;
proc sql;
select *
 from have
  group by group
   having n(value) ne 0;
quit;

View solution in original post


All Replies
Valued Guide
Posts: 596

Re: How to delete specific group of observations

It seems like you simply don't want the first 4 observations (which is easy to code) but I think you'll have to be specific about why you want them excluded for us to give you meaningful code

Occasional Contributor
Posts: 10

Re: How to delete specific group of observations

Posted in reply to ChrisBrooks

Thank you for your reply.

In value variable if the data is . from red1 to redn(lab variable) then I don't want that observations in the output.

if the variable value has . and any value(alphabets,dates,numerics)even at once,then I want the output for lab variable for red1 to redn observations for that specific occurrence (red1 to redn) only. Can you kindly let me know how to get this kind of output for huge data with unknown observations.Thank you.

Valued Guide
Posts: 631

Re: How to delete specific group of observations

[ Edited ]

Repeating a not very well described problem will most likely not yield better answers.

Other post: https://communities.sas.com/t5/General-SAS-Programming/how-to-remove-a-specific-range-of-observation...

@community_moderator Please merge the posts.

Occasional Contributor
Posts: 10

Re: How to delete specific group of observations

Posted in reply to andreas_lds

Hi,

I couldn't found the merge option to merge the posts.i have updated my reply. thank you.

PROC Star
Posts: 403

Re: How to delete specific group of observations

Try using lag(), or retain to keep variabel values across the observations and add logic to keep och remove records depending on previous records.

//Fredrik

Occasional Contributor
Posts: 10

Re: How to delete specific group of observations

Thank you for your reply. 

I tried but couldn't get the required output.if possible can you kindly send the sample program code.

Occasional Contributor
Posts: 10

Re: How to delete specific group of observations

Can anyone kindly help me in getting the required output,if possible with the syntax. Thank you.

Solution
‎02-23-2018 03:53 AM
Super User
Posts: 10,860

Re: How to delete specific group of observations

If I understood what you mean.

 

data ds;
input idno $ visit $  lab $value;
cards;
Su01 visit1 red1 .
Su01 visit1 red2 .
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red1 200
Su01 visit1 red2 10
Su01 visit1 red3 .
Su01 visit1 red4 .
Su01 visit1 red5 .
Su01 visit1 red6 .
Su01 visit1 red7 150
Su02 visit1 red1 300
Su02 visit1 red2 45
Su02 visit1 red3 .
Su02 visit1 red4 30
Su02 visit1 red1 200
Su02 visit1 red2 10
Su02 visit1 red3 .
Su02 visit1 red4 .
Su02 visit1 red5 150
Su02 visit1 red6 250
Su03 visit1 red1 .
Su03 visit1 red2 .
Su03 visit1 red3 .
Su03 visit1 red4 .
;
run;
data have;
 set ds;
 by idno;
 if first.idno or lab='red1' then group+1;
run;
proc sql;
select *
 from have
  group by group
   having n(value) ne 0;
quit;
Occasional Contributor
Posts: 10

Re: How to delete specific group of observations

A big thank you for your reply with syntax.it helped me so much.

Thank you again.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 147 views
  • 1 like
  • 5 in conversation