DATA Step, Macro, Functions and more

data manipulation

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

data manipulation

I have the following medicine dosage for each patient. I would like to set it missing once there is a 2nd treatment. See the new dosage column.

Patient IDMedicine datedosage# of dateTreatmentTreatment dateNew Dosage
11/13/20141327  132
11/14/20141328  132
11/15/20141329  132
11/16/201413210  132
11/17/201413211  132
11/18/201413212  132
11/19/201413213  132
11/20/201413214  132
11/21/201413215  132
11/22/201413216  132
11/23/201413217  132
11/24/201413218  132
11/25/201413219  132
11/26/201413220  132
11/27/201413221A27-Jan-14132
11/28/201413222 .132
11/29/201413223 .132
11/30/201441924A10-Feb-14 
11/31/201428625 . 
12/1/201428626 . 
12/2/201428627 . 
12/3/201428628 . 
12/4/201428629 . 
12/5/201428630 . 
12/6/201428631 . 
12/7/201428632 . 
12/8/201428633 . 
12/9/201428634 . 
12/10/201442135   

Accepted Solutions
Solution
‎06-17-2017 06:46 AM
Super User
Posts: 5,071

Re: data manipulation

Assuming your data is sorted as in your example, you could use:

 

data want;

set have;

by patient_id;

if first.patient_id then n_treatments = 0;

if treatment='A' then n_treatments + 1;

if n_treatments < 2 then new_dosage = dosage;

drop n_treatments;

run;

View solution in original post


All Replies
Super User
Posts: 10,466

Re: data manipulation

It isn't clear if the data you show is what you have or what you want. Please clearly indicate what your data looks like Before the manipulation and then After the manipulation.

 

It also helps if you can provide your example data in the form of a datastep so we can test code. When you paste values we have to write a data step to read the code and may make choices that do not match your data. For example I do not know for sure which, if any, of your varaibles are character or numeric. If I make an assumption of type and write code to handle that but your data differs it is very likely the solution I propose will not work.

 

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.

Solution
‎06-17-2017 06:46 AM
Super User
Posts: 5,071

Re: data manipulation

Assuming your data is sorted as in your example, you could use:

 

data want;

set have;

by patient_id;

if first.patient_id then n_treatments = 0;

if treatment='A' then n_treatments + 1;

if n_treatments < 2 then new_dosage = dosage;

drop n_treatments;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 98 views
  • 0 likes
  • 3 in conversation