DATA Step, Macro, Functions and more

subsetting a dataset based on same conditions on a numbered list variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

subsetting a dataset based on same conditions on a numbered list variables

Hi all!

 

I have a dataset at hand that looks like the following:

 

ID   MED1     MED2    MED3   MED4

1       892        384 .     454       345

2       802        394 .     434       233

3       852        384 .     334       599

 

I want to subset the dataset so that only patients with meds in {892, 334, 599, 384} remains. I don't want to repeat the list of 4 codes in all 4 variables in the data step. Can anyone show me how to do it? Thanks. 

 

 


Accepted Solutions
Solution
‎01-11-2018 07:37 PM
Super User
Posts: 23,937

Re: subsetting a dataset based on same conditions on a numbered list variables

Posted in reply to Viveme789

There are two approaches that work, there may be an easier one but IN didn't seem to work for me for two lists.

 

1. Create two arrays, loop through searching for a value, if found, output and exit the loop.

 

delete_array_method.JPG

 

The second approach transposes the data to a long format - much easier to use/query in the long run Smiley Happy

Then you can either keep it long or you could use that list to generate back the same answer as the first method.

 

delete_transpose_diag.JPG

View solution in original post


All Replies
Super User
Posts: 23,937

Re: subsetting a dataset based on same conditions on a numbered list variables

Posted in reply to Viveme789

Please don't post the same question multiple times. 

Are all your values numeric as in your example or are they character values?

Occasional Contributor
Posts: 18

Re: subsetting a dataset based on same conditions on a numbered list variables

all numeric

Solution
‎01-11-2018 07:37 PM
Super User
Posts: 23,937

Re: subsetting a dataset based on same conditions on a numbered list variables

Posted in reply to Viveme789

There are two approaches that work, there may be an easier one but IN didn't seem to work for me for two lists.

 

1. Create two arrays, loop through searching for a value, if found, output and exit the loop.

 

delete_array_method.JPG

 

The second approach transposes the data to a long format - much easier to use/query in the long run Smiley Happy

Then you can either keep it long or you could use that list to generate back the same answer as the first method.

 

delete_transpose_diag.JPG

Occasional Contributor
Posts: 18

Re: subsetting a dataset based on same conditions on a numbered list variables

Thank you so much! That's an nice answer. 

Super User
Posts: 2,041

Re: subsetting a dataset based on same conditions on a numbered list variables

Posted in reply to Viveme789

@Viveme789 Requesting you to please mark the question as answered and close the thread. Thank you!

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 121 views
  • 0 likes
  • 3 in conversation