BookmarkSubscribeRSS Feed
pransh
Calcite | Level 5

Each follow-up visit is represented as one record in the data set. Variables in the file are:
1. Patient id (1-50)
2. Treatment group ( 0 = control group, and 1 = experimental group)
3. Visit number, numbered consecutively for each subject
4. Time since last visit (months, with 0 at the first visit)
5. Stage of disease (0 = early stage, and 1 = after disease progression)
All subjects are in the early stage of disease at the first visit. Subjects remain in the study after disease progression, so there may be several records from a study patient after disease progression. The data are sorted by study visit, and then by patient id within study visit. There are no missing data.
1. Keeping data in a “one row for each visit” format (with multiple observations per patient) create a variable giving the number of months between the entry into the study for that subject and that study visit. (This will give the total number of months on study at each visit). PROC PRINT (e.g., the first 20 observations) a partial listing of the data to make sure that this new variable was created correctly.

 

i was trying to solve this question 

 

the code I have used is as follows 

 

filename in "E:\longdata.dat";
data temp;
infile in;
input pt_id trt_grp visits time stage ;
ttlmnth=time-visits;
run;

 

i m not sure whether I have done it correctly or not because I havent understood the question properly

 

can anyone help me out? @ballardw @PaigeMiller @Ric 

8 REPLIES 8
ballardw
Super User

Have you examined your data to see if it appears as expected after reading? Proc print or open the data set in table view.

 

To work with subjects by visit order you will need to sort the data by ID and visit number. (Proc sort).

 

Then in another data set you use the same BY variables as the sort. When you use a BY statement SAS provides automatic variables First. and Last. (the dots are important) with the variable name to indicate whether the current observation is the first or last of a by group. So by testing FIRST.Id variable you can set a counter to 0 or 1 (style choices). If the counter variable has been set as a RETAIN variable then the values will accumulate.

 

Without examples of the data we cannot tell if you have multiple visits per month, which means a simple counter won't work, or if there are gaps in the months which also means a simple counter will not work and you need to keep something else to calculate intervals with.

pransh
Calcite | Level 5
in question it has been given that the data is already sorted by study visit, and then by patient id within study visit. There are no missing data.

m not sure then why we will be using first. and last. thing
can u share ur email id , so that I can share a picture of dataset like how it looks so that it gives u a better idea
PaigeMiller
Diamond | Level 26

1. Keeping data in a “one row for each visit” format (with multiple observations per patient) create a variable giving the number of months between the entry into the study for that subject and that study visit. (This will give the total number of months on study at each visit). PROC PRINT (e.g., the first 20 observations) a partial listing of the data to make sure that this new variable was created correctly.

Is this asking for the cumulative sum of months since last visit? If so, you can create a cumulative sum like this:

 

data want;
    set have;
    by patient_id;
    if first.patient_id then cumulative_sum=0;
    cumulative_sum+time;
run;

 

 

can u share ur email id

 

No, you can't email it to me. You need to show us a portion of the data by typing it into your reply as working SAS data step code, or by following these instructions. Other ways to provide the data are not acceptable. Pictures are not acceptable here. Excel files are not acceptable here. People tend to ignore this statement that other ways to provide the data are not acceptable, please don't ignore it. We're trying to help you, but you have to help us too. So get used to providing the data this way and do it EVERY SINGLE TIME from now on.

--
Paige Miller
pransh
Calcite | Level 5
hi I did this but for next question
For a study like this, we want to report on the number of subjects in each group and how many total visits that there were per group. How many subjects and how many visits are there for each treatment group? (Hint: use PROC MEANS with two new created variables. The sum of 1s and 0s at the subject level is equal to the number of subjects. Also, what would the sum of 1s represent if the value, 1, is assigned for each observation?)

i was having same output for number of visits and no of subjects
the code is as follow
data temp;
infile in;
input pt_id trt_grp visits time stage ;
run;
proc sort data = temp;by pt_id;run;
data temp1;
set temp;
by pt_id;
if first.pt_id then totmnth=0;
totmnth+time;
run;
proc sort data=temp1;by trt_grp;run;
proc means data=temp1 noprint;
by trt_grp;
var pt_id visits;
output out=two n=total_pt total_visits;
run;


output : i am getting same number of subjects and visits for each trtr grp and I think that is wrong

Reeza
Super User

WAG

 

data temp;
infile in;
input pt_id trt_grp visits time stage ;
run;

proc sort data = temp;
by pt_id;
run;


data temp1;
set temp;
by pt_id;
if first.pt_id then totmnth=1;
else totmnth=0; run; proc sort data=temp1; by trt_grp; run; proc means data=temp1 noprint; by trt_grp; var totmnth visits; output out=two sum(totmnth) = unique_patients n(visits) = num_visits; run;

Depends on exactly how visits are coded. 

 

To count unique per group each patients first record is marked as one and then all else are marked as 0. I think you're calling it totmnth from a previous question that was accumulating a variable but the usage here is different. And this assumes that the trt_grp does not change between pt_id which if true, this code could be simplified. You can sort by trt_grp and pt_id at once and then do the summary by pt_id and not have to resort saving at least one step. 

pransh
Calcite | Level 5
hi,

i did this still its giving me same for both patients and visits


data temp;
infile in;
input pt_id trt_grp visits time stage ;
run;
proc sort data = temp;by pt_id;run;
data temp1;
set temp;
by pt_id;
if first.pt_id then totmnth=0;
totmnth+time;
run;
proc print data=temp1 (obs=20);run;
proc sort data=temp1;by trt_grp;run;
data temp1;
set temp;
by pt_id;
if first.pt_id then totmnth=0;
totmnth+time;
if first.pt_id then id=1;
else id=0;
run;
proc means data=temp1 noprint;
by trt_grp;
var id visits;
output out=two n=id total_visits;
run;


for your reference I have pasted both the questions here

1. Keeping data in a “one row for each visit” format (with multiple observations per patient) create a variable giving the number of months between the entry into the study for that subject and that study visit. (This will give the total number of months on study at each visit). PROC PRINT (e.g., the first 20 observations) a partial listing of the data to make sure that this new variable was created correctly.
2. For a study like this, we want to report on the number of subjects in each group and how many total visits that there were per group. How many subjects and how many visits are there for each treatment group? (Hint: use PROC MEANS with two new created variables. The sum of 1s and 0s at the subject level is equal to the number of subjects. Also, what would the sum of 1s represent if the value, 1, is assigned for each observation?)
PaigeMiller
Diamond | Level 26

Hello @pransh 

 

Please go back to your original post and provide a subject line that describes the problem in some meaningful way. Since almost every question here could be "programming help needed", that's not meaningful. Put a brief description of the problem in the subject line, please.

--
Paige Miller
pransh
Calcite | Level 5
doone

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2119 views
  • 2 likes
  • 4 in conversation