Hi everyone, this is a homework question I have been working on that I just can't get. We just went over working with duplicates and first. last., etc, so I'm thinking the solution would be found using this material. Here is the question (I wrote the data in using datalines):
PTID VisDate
1 11/05/2005
1 11/05/2005
1 02/15/2005
1 01/14/2004
5 05/05/2005
5 08/15/2007
2 05/15/2006
2 07/14/2006
2 01/05/2007
2 02/25/2007
3 05/05/2006
This is the output that I get with this code:
data earliestfifty; set qonetoo;
by ptidno apptdate;
if first.ptidno then nvisits = 0;
nvisits + 1;
if last.ptidno then totalvisits = nvisits;
run;
I tried merging this data set with the originally sorted data set like this:
data earliest;
merge earliestfifty qonetoo;
by ptidno;
run;
and get the same output. Is there something else I can do in the merge or the first data step to assign that total to each observation?
data qone; informat ptidno 3. apptdate date9.; input ptidno apptdate; format apptdate date9.; datalines; 1 05NOV2005 1 05NOV2005 1 15FEB2005 1 14JAN2004 5 05MAY2005 5 15AUG2007 2 15MAY2006 2 14JUL2006 2 05JAN2007 2 25FEB2007 3 05MAY2006 ; proc sort data = qone out = qonetoo; by ptidno apptdate; run; data earliestfifty; set qonetoo; by ptidno; if first.ptidno then nvisits = 0; nvisits + 1; run; proc sql; create table want as select *,count(*) as total from earliestfifty group by ptidno having nvisits<=total/2 order by ptidno,nvisits; run;
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.
Ready to level-up your skills? Choose your own adventure.