BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
martyvd
Fluorite | Level 6
 

Hello, I have data structured like this: 

 

IDtest_datetest_result
127Mar1992N
108Dec1999P
129Jan2005N
213Jan2015N
209Mar2017P
205Jun2018P
315Oct1996N
305Sep1997N
328Jun1998N
 
I need to keep all records for each ID if they had a test_result=P that occurred within 2017-2018. For this example only records from ID 2 would be kept. 
 
Thank you! 
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@martyvd wrote:
This code does not give me any errors in the log but it produces a dataset with no rows. Not sure why.

Did you make sure to change the data set names to match yours, and the date variable name?

When I run:

data have;
input ID	test_date :date9.	test_result $;
format test_date date9.;
datalines;
1	27Mar1992	N
1	08Dec1999	P
1	29Jan2005	N
2	13Jan2015	N
2	09Mar2017	P
2	05Jun2018	P
3	15Oct1996	N
3	05Sep1997	N
3	28Jun1998	N
;

proc sql;
create table want as 
select * from have
where ID in (select id from have where test_result='P' and year(test_date) in (2017 2018) )
;
quit; 

I get the 3 records with ID=2.

 

Post your log from running the code.

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

data want;
   set have;
   where year(test_date) in (2017:2018);
run;
martyvd
Fluorite | Level 6
Peter, maybe I should have clarified. I need to subset records for only IDs that had a positive test result (test_result=P) in 2017-2018. If an ID had a positive test in this time period, I need to keep all other records for that ID as well.
PeterClemmensen
Tourmaline | Level 20

Ah ok. There you go.

 

data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

data want;

   if _N_ = 1 then do;
      dcl hash h(dataset : "have(where=(year(test_date) in (2017:2018)))");
      h.definekey("ID");
      h.definedone();
   end;

   set have;
   
   if h.check() = 0;
run;
Tom
Super User Tom
Super User

I need to keep all records for each ID if they had a test_result=P that occurred within 2017-2018. For this example only records from ID 2 would be kept. 

proc sql;
create table want as 
select * from have
where ID in (select id from have where test_result='P' and year(date) in (2017 2018) )
;
quit;

 

 

martyvd
Fluorite | Level 6
This code does not give me any errors in the log but it produces a dataset with no rows. Not sure why.
ballardw
Super User

@martyvd wrote:
This code does not give me any errors in the log but it produces a dataset with no rows. Not sure why.

Did you make sure to change the data set names to match yours, and the date variable name?

When I run:

data have;
input ID	test_date :date9.	test_result $;
format test_date date9.;
datalines;
1	27Mar1992	N
1	08Dec1999	P
1	29Jan2005	N
2	13Jan2015	N
2	09Mar2017	P
2	05Jun2018	P
3	15Oct1996	N
3	05Sep1997	N
3	28Jun1998	N
;

proc sql;
create table want as 
select * from have
where ID in (select id from have where test_result='P' and year(test_date) in (2017 2018) )
;
quit; 

I get the 3 records with ID=2.

 

Post your log from running the code.

martyvd
Fluorite | Level 6
There was a problem with my data that has been resolved. Now the code seems to be working, thanks.
novinosrin
Tourmaline | Level 20

Hi @martyvd 


data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

proc sql;
 create table want as
 select *
 from have 
 group by id
 having sum((test_resultb='P') and year(test_date) in (2017,2018));
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 656 views
  • 2 likes
  • 5 in conversation