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

I have a dataset with subject ,month and treatment.

I want to figure out how to calculate the subject with the following criteria:

The subjects have been in the treatment for the whole time during the year in every month. If the subject has been given a placebo in between then i need to account only those months that he receives drug after the break when he received the placebo.for a year for the months 1-12 .

usubjid   months  treatment

usubid1     01      drug

usubid1     02     placebo

usubjid 1   03      drug and then he is on drug continuously.

so i want in my dataset usubjid1 from month 3 till 12.

the dataset is sorted in this order usubjid/month

1 ACCEPTED SOLUTION

Accepted Solutions
Scott_Mitchell
Quartz | Level 8

Is this more suitable?  I changed the data you provided slightly so that the results could be better displayed.

DATA HAVE;

LENGTH TREATMENTS $4;

INFILE DATALINES DELIMITER=',';

INPUT USUBJID $ MONTH $ TREATMENTS $;

DATALINES;

USUBJID1,01,DRUG

USUBJID1,02,DRUG

USUBJID1,04,DRUG

USUBJID2,05,DRUG

USUBJID2,06,DRUG

USUBJID1,10,DRUG

USUBJID1,11,DRUG

USUBJID1,12,DRUG

USUBJID2,11,DRUG

;

RUN;

PROC SORT DATA = HAVE /*(DROP = TREATMENTS)*/;

  BY USUBJID MONTH;

RUN;

PROC TRANSPOSE DATA = HAVE OUT=TRANSHAVE (DROP=_NAME_);

  BY USUBJID;

  ID MONTH;

  VAR TREATMENTS;

RUN;

DATA LASTCONSEC (DROP = '01'N '02'N '03'N '04'N '05'N '06'N '07'N '08'N '09'N '10'N '11'N '12'N);

  ATTRIB '01'N '02'N '03'N '04'N '05'N '06'N '07'N '08'N '09'N '10'N '11'N '12'N LENGTH = $4;

  SET TRANSHAVE;

 

  ARRAY TRANSVARS '01'N-'12'N;

  DO I = 1 TO 12;

   IF I = 1 THEN DO;

     IF TRANSVARS{I} = "DRUG" THEN STARTMTH = VNAME(TRANSVARS{I});

  IF TRANSVARS{I} = "DRUG" THEN ENDMTH = VNAME(TRANSVARS{I});

   END;

   ELSE DO;

      IF TRANSVARS{I-1} = "" AND TRANSVARS{I} ~= "" THEN STARTMTH = VNAME(TRANSVARS{I});

  IF TRANSVARS{I} = "DRUG" THEN ENDMTH = VNAME(TRANSVARS{I});

   END;

  END;

  DROP I;

RUN;

DATA WANT;

  MERGE LASTCONSEC (IN=A)

   HAVE (IN=B);

  BY USUBJID;

  IF A AND B

  AND STARTMTH <= MONTH <= ENDMTH;

  DROP STARTMTH ENDMTH;

RUN;

View solution in original post

6 REPLIES 6
ballardw
Super User

Do you have multiple years in your data? For example, would a subject have months in order 10, 11, 12, 01, ... 09 to represent one year of data?

mkpk
Calcite | Level 5

Thank you for getting back. It is one year. no multiple years.

Scott_Mitchell
Quartz | Level 8

Hi MKPK,

Does this do what you are after?

DATA HAVE;

INFILE DATALINES DELIMITER=',';

INPUT USUBJID $ MONTH $ TREATMENTS $;

DATALINES; 

USUBJID1,01,DRUG

USUBJID1,02,PLACEBO

USUBJID1,03,DRUG

USUBJID1,04,DRUG

USUBJID1,05,DRUG

USUBJID1,06,DRUG

USUBJID1,07,DRUG

USUBJID1,08,DRUG

USUBJID1,09,DRUG

USUBJID1,10,DRUG

USUBJID1,11,DRUG

USUBJID1,12,DRUG

;

RUN;

PROC SORT DATA=HAVE;

BY USUBJID MONTH;

RUN;

DATA WANT;

  MERGE HAVE (IN=A WHERE=(PTREATMENTS = "PLACEBO") RENAME = (MONTH = PMONTH TREATMENTS = PTREATMENTS))

   HAVE (IN=B);

  BY USUBJID;

  IF A AND B AND MONTH > PMONTH;

  DROP PMONTH PTREATMENTS;

RUN;

mkpk
Calcite | Level 5

Hi

Thank you for getting back to me.

My dataset has only treatment in it but there are gap in the months that the subject receives drug and i need only those subjects who have remained on drug 100 % after they have come back from the gap.

DATA HAVE;

INFILE DATALINES DELIMITER=',';

INPUT USUBJID $ MONTH $ TREATMENTS $;

DATALINES;

USUBJID1,01,DRUG

USUBJID1,02,drug

USUBJID1,04,DRUG

USUBJID2,05,DRUG

USUBJID2 06,DRUG

USUBJID1,10,DRUG

USUBJID1,11,DRUG

USUBJID1,12,DRUG

USUBJID2,10,DRUG

USUBJID2,11,DRUG

USUBJID2,12,DRUG

;

RUN;

From from the dataset above as you see all trt is same  but i want only from  the count of subjied from week 10-12 as well as subjid 2 from week 10-12 only.

Thankx a lot

Scott_Mitchell
Quartz | Level 8

Is this more suitable?  I changed the data you provided slightly so that the results could be better displayed.

DATA HAVE;

LENGTH TREATMENTS $4;

INFILE DATALINES DELIMITER=',';

INPUT USUBJID $ MONTH $ TREATMENTS $;

DATALINES;

USUBJID1,01,DRUG

USUBJID1,02,DRUG

USUBJID1,04,DRUG

USUBJID2,05,DRUG

USUBJID2,06,DRUG

USUBJID1,10,DRUG

USUBJID1,11,DRUG

USUBJID1,12,DRUG

USUBJID2,11,DRUG

;

RUN;

PROC SORT DATA = HAVE /*(DROP = TREATMENTS)*/;

  BY USUBJID MONTH;

RUN;

PROC TRANSPOSE DATA = HAVE OUT=TRANSHAVE (DROP=_NAME_);

  BY USUBJID;

  ID MONTH;

  VAR TREATMENTS;

RUN;

DATA LASTCONSEC (DROP = '01'N '02'N '03'N '04'N '05'N '06'N '07'N '08'N '09'N '10'N '11'N '12'N);

  ATTRIB '01'N '02'N '03'N '04'N '05'N '06'N '07'N '08'N '09'N '10'N '11'N '12'N LENGTH = $4;

  SET TRANSHAVE;

 

  ARRAY TRANSVARS '01'N-'12'N;

  DO I = 1 TO 12;

   IF I = 1 THEN DO;

     IF TRANSVARS{I} = "DRUG" THEN STARTMTH = VNAME(TRANSVARS{I});

  IF TRANSVARS{I} = "DRUG" THEN ENDMTH = VNAME(TRANSVARS{I});

   END;

   ELSE DO;

      IF TRANSVARS{I-1} = "" AND TRANSVARS{I} ~= "" THEN STARTMTH = VNAME(TRANSVARS{I});

  IF TRANSVARS{I} = "DRUG" THEN ENDMTH = VNAME(TRANSVARS{I});

   END;

  END;

  DROP I;

RUN;

DATA WANT;

  MERGE LASTCONSEC (IN=A)

   HAVE (IN=B);

  BY USUBJID;

  IF A AND B

  AND STARTMTH <= MONTH <= ENDMTH;

  DROP STARTMTH ENDMTH;

RUN;

mkpk
Calcite | Level 5

Thank you Very much.

This has helped me a lot.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1655 views
  • 3 likes
  • 3 in conversation