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

I have a dataset with multiple timepoints and every visit has timepoint collected 3 times and if there is any timepoint that is collected more than 3 times it should come in output. I had written below code but it is not working. I had transposed all timepoints and got them into one variable called timepoint.

data abc_1;
	set abc;
	by SubjectId visit timepoint;
	Seq+1;
	if first.timepoint then Seq=1;
	if seq>3 then flag=1;
run;

example dataset as below

data ndsn;
infile datalines;
input subject visit $4. TP1 TP2 TP3;
datalines;
101 Day1 1  1  1
101 Day1 1  2  2
101 Day1 3  3  3
101 Day1 4  4  4
101 Day1 7  7  7
101 Day1 7  8  8
101 Day1 9  9  9
101 Day1 9 10 10
101 Day1 11 11 11
101 Day2 1  1  1
101 Day2 2  2  2
101 Day2 2  3  3
101 Day2 4  4  4
101 Day2 5  5  5
101 Day2 5  6  6
101 Day2 7  7  7
101 Day2 10 10 10
101 Day2 11 10 11
;
run;

desired output as below after transposing the 3 variables to one variable called timepoint i got that variable

data ndsn;
infile datalines;
input subject visit $4. timepoint;
datalines;
101 Day1 1 
101 Day1 7 
101 Day1 9
101 Day2 2 
101 Day2 5 
101 Day2 10
;
run;

Any help please

1 ACCEPTED SOLUTION

Accepted Solutions
Oligolas
Barite | Level 11

Hi,

you could use SQL to achieve this:

PROC SQL;
   CREATE TABLE ndsn_t AS
      SELECT subject,visit,TP,count(TP) AS COUNT
      FROM
        (SELECT subject,visit,TP1 AS TP FROM ndsn
         UNION CORRESPONDING ALL 
         SELECT subject,visit,TP2 AS TP FROM ndsn
         UNION CORRESPONDING ALL 
         SELECT subject,visit,TP3 AS TP FROM ndsn)
      GROUP BY subject,visit,TP
      HAVING COUNT>3
      ORDER BY subject,visit,TP
   ;
QUIT;
________________________

- Cheers -

View solution in original post

5 REPLIES 5
Oligolas
Barite | Level 11

Hi,

you could use SQL to achieve this:

PROC SQL;
   CREATE TABLE ndsn_t AS
      SELECT subject,visit,TP,count(TP) AS COUNT
      FROM
        (SELECT subject,visit,TP1 AS TP FROM ndsn
         UNION CORRESPONDING ALL 
         SELECT subject,visit,TP2 AS TP FROM ndsn
         UNION CORRESPONDING ALL 
         SELECT subject,visit,TP3 AS TP FROM ndsn)
      GROUP BY subject,visit,TP
      HAVING COUNT>3
      ORDER BY subject,visit,TP
   ;
QUIT;
________________________

- Cheers -

Ravindra_
Quartz | Level 8
Thanks a lot for the solution, i will try this and will let you know if i need any help further
PeterClemmensen
Tourmaline | Level 20

Try this

 

data ndsn;
infile datalines;
input subject visit $4. TP1 TP2 TP3;
datalines;
101 Day1 1  1  1
101 Day1 1  2  2
101 Day1 3  3  3
101 Day1 4  4  4
101 Day1 7  7  7
101 Day1 7  8  8
101 Day1 9  9  9
101 Day1 9 10 10
101 Day1 11 11 11
101 Day2 1  1  1
101 Day2 2  2  2
101 Day2 2  3  3
101 Day2 4  4  4
101 Day2 5  5  5
101 Day2 5  6  6
101 Day2 7  7  7
101 Day2 10 10 10
101 Day2 11 10 11
;
run;

data temp(drop = tp:);
   set ndsn;
   array t tp:;
   do over t;
      timepoint = t;
      output;
   end;
run;

proc summary data = temp nway;
   class subject visit timepoint;
   var timepoint;
   output out = want(where = (_freq_ > 3)) n =;
run;
Ravindra_
Quartz | Level 8
Thanks for the response, i will try this as well and will let you know for any support
Ravindra_
Quartz | Level 8

Although all the suggestions were useful. @Oligolas  i had used your suggestion in my program and it worked for me. thanks a lot for your time everyone

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
  • 5 replies
  • 527 views
  • 0 likes
  • 3 in conversation