Removing a variable conditionally

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Removing a variable conditionally

Dear all,

 

How can I remove observations that contain a specific character?

 

For example, remove observations contain E

 

ID    DX     Diag_Sequence

1      96         1

1      E96       1

1        87        2

1        E87      2

1       V87      3 

 

 

Best,

 

Liang  

 


Accepted Solutions
Solution
‎03-15-2018 03:07 PM
Respected Advisor
Posts: 2,994

Re: Removing a variable conditionally

Posted in reply to Jackwangliang
if find(dx,'E','i')>0 then delete;
--
Paige Miller

View solution in original post


All Replies
Solution
‎03-15-2018 03:07 PM
Respected Advisor
Posts: 2,994

Re: Removing a variable conditionally

Posted in reply to Jackwangliang
if find(dx,'E','i')>0 then delete;
--
Paige Miller
Occasional Contributor
Posts: 17

Re: Removing a variable conditionally

Posted in reply to PaigeMiller
Great! Thank you!
Occasional Contributor
Posts: 17

Re: Removing a variable conditionally

Posted in reply to PaigeMiller
Thank you all for your great replies!



What if remove E that the Diag_Sequence is the same as previous observation, but do not remove E that has different Diag_Sequence number, for example,



ID DX Diag_Sequence

1 96 1

1 E96 1 (remove)

1 87 2

1 E87 2 (remove)

1 V87 3

1 E23 4 (do not remove)

1 12 5

1 15 6

1 E15 6 (remove)



I am a new SAS user, thank you very much for helping.



Liang
Super User
Posts: 23,689

Re: Removing a variable conditionally

Posted in reply to Jackwangliang

It depends on how complicated it gets.

 

If its a single value you can chain logic together, such as 

if dx=:’E’ and dx ne ‘E23’ then delete;

 

But if the logic gets too complex you need a different approach. Do you have those lists of what to include/exclude in a data set or are they in your head? If you make them into a data set an entirely different approach is warranted, which may be easier overall.

Occasional Contributor
Posts: 17

Re: Removing a variable conditionally

 

Hi, Reeza. Thanks for your comment. 

Here is a screenshot of part of my dataset. Variable Diagnosis_Code may contain observations with E, for example, E9505.

For this E9505, if its Diagnosis_Sequence is the same as previous observations, then delete this E9505 observation. But if this Diagnosis_Sequence is not the same as the previous one, keep it. How can I code this? 

 

Best,

 

Liang

 

 

picture 1.PNG

Super User
Posts: 23,689

Re: Removing a variable conditionally

Posted in reply to Jackwangliang

@Jackwangliang thats not at all like your initial question. 

 

Please post data as text, I won't type out your data, and what you would expect as output. Since this is so different from your original question, I would suggest you start a new thread. 

 


@Jackwangliang wrote:

 

Hi, Reeza. Thanks for your comment. 

Here is a screenshot of part of my dataset. Variable Diagnosis_Code may contain observations with E, for example, E9505.

For this E9505, if its Diagnosis_Sequence is the same as previous observations, then delete this E9505 observation. But if this Diagnosis_Sequence is not the same as the previous one, keep it. How can I code this? 

 

Best,

 

Liang

 

 

picture 1.PNG


 

Occasional Contributor
Posts: 17

Re: Removing a variable conditionally

How can I post my data as text here? Thanks.
Super User
Posts: 23,689

Re: Removing a variable conditionally

Posted in reply to Jackwangliang
Contributor
Posts: 67

Re: Removing a variable conditionally

Posted in reply to Jackwangliang

proc sql;

delete from table_name

where DX contains 'E';

Michelle
Super User
Posts: 23,689

Re: Removing a variable conditionally

Posted in reply to Jackwangliang
If dx =: ‘E’ then delete; *removes all dx that start with E;
If find(dx, ‘E’, ‘i’)>0 then delete;*removes all dx that contain an E;

Use find to search for an E, there’s a possibility I have the parameters backwards in the FIND() function so you should verify that. 

 

Or use =: which checks if the first characters are the same. 

 


@Jackwangliang wrote:

Dear all,

 

How can I remove observations that contain a specific character?

 

For example, remove observations contain E

 

ID    DX     Diag_Sequence

1      96         1

1      E96       1

1        87        2

1        E87      2

1       V87      3 

 

 

Best,

 

Liang  

 


 

Occasional Contributor
Posts: 17

Re: Removing a variable conditionally

Posted in reply to Jackwangliang

Thank you all for your great replies! 

 

What if remove E that the Diag_Sequence is the same as previous observation, but do not remove E that has different Diag_Sequence number, for example,

 

ID    DX     Diag_Sequence

1      96         1

1      E96       1     (remove)

1        87        2

1        E87      2    (remove)

1       V87      3 

1      E23       4     (do not remove)

1      12          5

1      15        6

1      E15     6      (remove)

 

I am a new SAS user, thank you very much for helping.

 

Liang

 

☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 165 views
  • 1 like
  • 4 in conversation