DATA Step, Macro, Functions and more

summarize data

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

summarize data

[ Edited ]

Hi, 

I have problem with summarizing my data. I need to count how many distinct patient_Name for each distinct time point  if date_completed ne . 

I tried my code put I really missed up with it. 

I need you to suggest an edit to my code.  

proc sort data=readert1; by patient_name TimePoint; run;
data readert2;
set readert1;
by patient_name timepoint;
keep Patient_Name TimePoint Date_Completed ctime;
if date_completed ne . and first.Patient_name then do; ctime=0; end;
ctime+1;
if date_completed ne . and last.timepoint ;

this is the data that I need to summarize 

 

 Obs Patient_Name Timepoint Date_Completed1234567891011121314151617181920212223242526272829303132333435363738394041

W1490.1045001Baseline15DEC2017
W1490.1056002Baseline13NOV2017
W1490.1056002Baseline13NOV2017
W1490.1056002Week 1615NOV2017
W1490.1056002Week 1615NOV2017
W1490.1056002Week 2431JAN2018
W1490.1056002Week 2431JAN2018
W1490.1056002Week 815NOV2017
W1490.1056002Week 815NOV2017
W1490.1056003Baseline13NOV2017
W1490.1056003Week 813NOV2017
W1490.1056005Baseline13NOV2017
W1490.1056005Week 1613NOV2017
W1490.1056005Week 2425DEC2017
W1490.1056005Week 813NOV2017
W1490.1056006Baseline12DEC2017
W1490.1056006Week 1612DEC2017
W1490.1056006Week 2412JAN2018
W1490.1056006Week 812DEC2017
W1490.1068001Baseline13NOV2017
W1490.1068001Baseline13NOV2017
W1490.1068001Week 1613DEC2017
W1490.1068001Week 1613DEC2017
W1490.1068001Week 2425DEC2017
W1490.1068001Week 2425DEC2017
W1490.1068001Week 3222FEB2018
W1490.1068001Week 3222FEB2018
W1490.1068001Week 813NOV2017
W1490.1068001Week 813NOV2017
W1490.1068002Baseline.
W1490.1068002Week 8.
W1490.1089001Baseline15NOV2017
W1490.1089001Week 1615NOV2017
W1490.1089001Week 2418DEC2017
W1490.1089001Week 3210JAN2018
W1490.1089001Week 815NOV2017
W1490.1089002Baseline15DEC2017
W1490.1089003Baseline28DEC2017
W1490.1100002Baseline31JAN2018
W1490.1134001Baseline.
W1490.1134004Baseline30JAN2018

Accepted Solutions
Solution
‎03-13-2018 03:38 PM
Regular Contributor
Posts: 161

Re: summarize data

I just found a way by using retain statement

View solution in original post


All Replies
PROC Star
Posts: 1,316

Re: summarize data

Show us the code you've used so far, and maybe someone can suggest the necessary changes.

 

Tom

Regular Contributor
Posts: 161

Re: summarize data

I just added the code
Highlighted
Super User
Posts: 23,771

Re: summarize data

Regular Contributor
Posts: 161

Re: summarize data

I need to do it in the data step 

Super User
Posts: 13,583

Re: summarize data


@mona4u wrote:

I need to do it in the data step 


Why? If a proc available to basically everyone works why the restriction.

 

You should show what the result for your example data would be. If you want to count names within timepoint then the sort and by processing likely should be BY Timepoint patient_name;

Perhaps something like this untested code.

proc sort data=readert1; 
   by timepoint patient_name ; 
run;
data readert2;
   set readert1 (where=(date_completed ne .));
   by timepoint patient_name ; 
   keep Patient_Name TimePoint Date_Completed ctime;
   if first.timepoint then patientcount=0;
   if first.patient then patientcount+1;
   if last.timepoint;
run;

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Regular Contributor
Posts: 161

Re: summarize data

this code doesn't summarize that data correctly 

Super User
Posts: 13,583

Re: summarize data


@mona4u wrote:

this code doesn't summarize that data correctly 


Since you did not show any desired result I don't think that is very responsive.

 

Show the correct summary for example data.

Regular Contributor
Posts: 161

Re: summarize data

this code doesn't summarize that data correctly. 

appreciate your try

Respected Advisor
Posts: 3,055

Re: summarize data

[ Edited ]
proc freq data=have(where=(date_completed^=.));
    tables patient_name/out=_a_;
run;

The number of observations in _a_ is the number of distinct patient_name exist in your data.

--
Paige Miller
Regular Contributor
Posts: 161

Re: summarize data

Posted in reply to PaigeMiller

that's not right I want the number of distinct patient_name for each distinct timepoint 

Solution
‎03-13-2018 03:38 PM
Regular Contributor
Posts: 161

Re: summarize data

I just found a way by using retain statement
Super User
Posts: 23,771

Re: summarize data

The SUM statement implies RETAIN. 

 

 

Super User
Posts: 13,583

Re: summarize data


@mona4u wrote:
I just found a way by using retain statement

Show your actual working result code in the post you marked correct so that someone searching the forum for help can see the approach taken.

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 194 views
  • 1 like
  • 5 in conversation