BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SDasari2
Calcite | Level 5
IDDIAGNOSISCODELIST_IDISADMITDIAGNOSISDIAGNOSISCODEPXEXTRACTDATETIME
CR-81442011J96.2103MAY2019:00:00:22.000000
CR-81442011J96.2111MAY2019:00:00:22.000000
CR-8144202.J44.103MAY2019:00:00:22.000000
CR-8144202.J44.111MAY2019:00:00:22.000000
CR-8144203.A41.903MAY2019:00:00:22.000000
CR-8144203.A41.911MAY2019:00:00:22.000000
CR-8144204.J18.903MAY2019:00:00:22.000000
CR-8144204.J18.911MAY2019:00:00:22.000000

 

I have a dataset like this and i need to covert it to 1 row per ID like the below. I need to take the max of the PXEXTRACTDATETIME for each ID/DIAGNOSISCODELIST_ID_1. Please advise the best way and i have also attached the example in an example.

 

Want            
             
IDDIAGNOSISCODELIST_ID_1ISADMITDIAGNOSIS_1DIAGNOSISCODE_1DIAGNOSISCODELIST_ID_2ISADMITDIAGNOSIS_2DIAGNOSISCODE_2DIAGNOSISCODELIST_ID_3ISADMITDIAGNOSIS_3DIAGNOSISCODE_3DIAGNOSISCODELIST_ID_4ISADMITDIAGNOSIS_4DIAGNOSISCODE_4
CR-81442011J96.212.J44.13.A41.94.J18.9
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hello @SDasari2 

 

data have;
input ID :$30.	DIAGNOSISCODELIST_ID	ISADMITDIAGNOSIS	DIAGNOSISCODE :$15.	PXEXTRACTDATETIME :datetime40.;
format PXEXTRACTDATETIME datetime40.;
cards;
CR-814420	1	1	J96.21	03MAY2019:00:00:22.000000
CR-814420	1	1	J96.21	11MAY2019:00:00:22.000000
CR-814420	2	.	J44.1	03MAY2019:00:00:22.000000
CR-814420	2	.	J44.1	11MAY2019:00:00:22.000000
CR-814420	3	.	A41.9	03MAY2019:00:00:22.000000
CR-814420	3	.	A41.9	11MAY2019:00:00:22.000000
CR-814420	4	.	J18.9	03MAY2019:00:00:22.000000
CR-814420	4	.	J18.9	11MAY2019:00:00:22.000000
;

proc sql;
create table temp as
select * 
from have
group by ID,DIAGNOSISCODELIST_ID
having max(PXEXTRACTDATETIME)=PXEXTRACTDATETIME;
quit;

proc sql noprint; 
select max(obs) into :obs
from (select count(*) as obs  from temp group by ID ) ;
quit;
%put &=obs;

proc summary nway data=temp missing; 
 class ID ; 
 output 
 out = work.wide(drop=_type_ _freq_) 
 idgroup(out[&obs](DIAGNOSISCODELIST_ID ISADMITDIAGNOSIS DIAGNOSISCODE)=) 
 ;
run;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@SDasari2 wrote:


I have a dataset like this and i need to covert it to 1 row per ID like the below. I need to take the max of the PXEXTRACTDATETIME for each ID/DIAGNOSISCODELIST_ID_1. Please advise the best way and i have also attached the example in an example.



If this is your goal, there's no need to reformat the data at all. The maximum PXEXTRACTDATETIME can be found using PROC SUMMARY.

 

proc summary data=have nway;
    class id diagnosiscodelist_id;
    var pxextractdatetime;
    output out=want max=;
run;

 

For most analyses (not all, but most) reformatting a long data set to wide is an unnecessary and time consuming step. 

--
Paige Miller
SDasari2
Calcite | Level 5

This give the max of pxextractdatetime with the 4 differnt DIAGNOSISCODELIST_ID one below the other. I need all of them in one row for the given ID.

ballardw
Super User

@SDasari2 wrote:
ID DIAGNOSISCODELIST_ID ISADMITDIAGNOSIS DIAGNOSISCODE PXEXTRACTDATETIME
CR-814420 1 1 J96.21 03MAY2019:00:00:22.000000
CR-814420 1 1 J96.21 11MAY2019:00:00:22.000000
CR-814420 2 . J44.1 03MAY2019:00:00:22.000000
CR-814420 2 . J44.1 11MAY2019:00:00:22.000000
CR-814420 3 . A41.9 03MAY2019:00:00:22.000000
CR-814420 3 . A41.9 11MAY2019:00:00:22.000000
CR-814420 4 . J18.9 03MAY2019:00:00:22.000000
CR-814420 4 . J18.9 11MAY2019:00:00:22.000000

 

I have a dataset like this and i need to covert it to 1 row per ID like the below. I need to take the max of the PXEXTRACTDATETIME for each ID/DIAGNOSISCODELIST_ID_1. Please advise the best way and i have also attached the example in an example.

 

Want                        
                         
ID DIAGNOSISCODELIST_ID_1 ISADMITDIAGNOSIS_1 DIAGNOSISCODE_1 DIAGNOSISCODELIST_ID_2 ISADMITDIAGNOSIS_2 DIAGNOSISCODE_2 DIAGNOSISCODELIST_ID_3 ISADMITDIAGNOSIS_3 DIAGNOSISCODE_3 DIAGNOSISCODELIST_ID_4 ISADMITDIAGNOSIS_4 DIAGNOSISCODE_4
CR-814420 1 1 J96.21 2 . J44.1 3 . A41.9 4 . J18.9

Do you want the max or are you looking to "dedup" the records keeping the latest PXEXTRACTDATETIME record for each ID Diagnosiscodelist_id?

SDasari2
Calcite | Level 5

I want the max(pxextractdatetime) for each ID/DIAGNOSISCODELIST_ID.

novinosrin
Tourmaline | Level 20

Hello @SDasari2 

 

data have;
input ID :$30.	DIAGNOSISCODELIST_ID	ISADMITDIAGNOSIS	DIAGNOSISCODE :$15.	PXEXTRACTDATETIME :datetime40.;
format PXEXTRACTDATETIME datetime40.;
cards;
CR-814420	1	1	J96.21	03MAY2019:00:00:22.000000
CR-814420	1	1	J96.21	11MAY2019:00:00:22.000000
CR-814420	2	.	J44.1	03MAY2019:00:00:22.000000
CR-814420	2	.	J44.1	11MAY2019:00:00:22.000000
CR-814420	3	.	A41.9	03MAY2019:00:00:22.000000
CR-814420	3	.	A41.9	11MAY2019:00:00:22.000000
CR-814420	4	.	J18.9	03MAY2019:00:00:22.000000
CR-814420	4	.	J18.9	11MAY2019:00:00:22.000000
;

proc sql;
create table temp as
select * 
from have
group by ID,DIAGNOSISCODELIST_ID
having max(PXEXTRACTDATETIME)=PXEXTRACTDATETIME;
quit;

proc sql noprint; 
select max(obs) into :obs
from (select count(*) as obs  from temp group by ID ) ;
quit;
%put &=obs;

proc summary nway data=temp missing; 
 class ID ; 
 output 
 out = work.wide(drop=_type_ _freq_) 
 idgroup(out[&obs](DIAGNOSISCODELIST_ID ISADMITDIAGNOSIS DIAGNOSISCODE)=) 
 ;
run;
SDasari2
Calcite | Level 5

Thanks a lot. This works!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 6 replies
  • 1261 views
  • 0 likes
  • 4 in conversation