BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sri_devi
Obsidian | Level 7

Hello experts,

 

i have below data as. My question here is how to get the dose data value to all the missing data row in the new dose column. for example i want for 1001 subject the dose 2.5 should be reflected to the row above to it on the new dose column. Please refer the required output as in table 2

 

table 1:

SubjectIDDose New dose 
1001. 
1001. 
1001. 
10012.55
1001. 
1001. 
1001. 
100157.5
1001. 
1001. 
10017.512.5
1002. 
1002. 
1002. 
10021012.5
1002. 
1002. 
100212.57.5
1003. 
1003. 
1003. 
1003105
1003. 
1003. 

 

i want the data as in below table

table 2:

SubjectIDDose New dose 
1001.2.5
1001.2.5
1001.2.5
10012.55
1001.5
1001.5
1001.5
100157.5
1001.7.5
1001.7.5
10017.512.5
1002.10
1002.10
1002.10
10021012.5
1002.12.5
1002.12.5
100212.57.5
1003.10
1003.10
1003.10
1003105
1003.5
1003.5

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input SubjectID	Dose 	New_dose ;
cards;
1001	.	 
1001	.	 
1001	.	 
1001	2.5	5
1001	.	 
1001	.	 
1001	.	 
1001	5	7.5
1001	.	 
1001	.	 
1001	7.5	12.5
1002	.	 
1002	.	 
1002	.	 
1002	10	12.5
1002	.	 
1002	.	 
1002	12.5	7.5
1003	.	 
1003	.	 
1003	.	 
1003	10	5
1003	.	 
1003	.	 
;

data want;
if 0 then set have;
 do until(last.SubjectID or not missing(New_dose));
   set have;
   by SubjectID;
   retain _Dose _New_dose _lag_New_dose;
   if first.SubjectID then first=1;
   if not missing(Dose) then _Dose=Dose;
   if not missing(New_dose) then _New_dose=New_dose;
 end;

  do until(last.SubjectID or not missing(New_dose));
   set have;
   by SubjectID;
   if missing(New_dose) then do;
     if first then New_dose2=_Dose;
	  else New_dose2=_lag_New_dose;
   end;
   else New_dose2=_New_dose;
   output;
 end;
 _lag_New_dose=_New_dose;
 drop first _:;
run;

View solution in original post

10 REPLIES 10
HB
Barite | Level 11 HB
Barite | Level 11
Is there an ordering variable we don't see? Date of observation or sequential observation number?
Sri_devi
Obsidian | Level 7

Hello, yes we subject seq & dose date which i have mentioned in below tables :

table 1:    
SubjectIDsubject seqdose dateDose New dose 
1001115/07/2021. 
1001116/07/2021. 
1001117/07/2021. 
1001118/07/20212.55
1001119/07/2021. 
1001120/07/2021. 
1001121/07/2021. 
1001122/07/202157.5
1001123/07/2021. 
1001124/07/2021. 
1001125/07/20217.512.5
1002226/07/2021. 
1002227/07/2021. 
1002228/07/2021. 
1002229/07/20211012.5
1002230/07/2021. 
1002231/07/2021. 
100221/08/202112.57.5
100332/08/2021. 
100333/08/2021. 
100334/08/2021. 
100335/08/2021105
100336/08/2021. 
100337/08/2021. 

 

 

 

 

i want this output:

table 2:    
SubjectIDsubject seqdose dateDose New dose 
1001115/07/2021.2.5
1001116/07/2021.2.5
1001117/07/2021.2.5
1001118/07/20212.55
1001119/07/2021.5
1001120/07/2021.5
1001121/07/2021.5
1001122/07/202157.5
1001123/07/2021.7.5
1001124/07/2021.7.5
1001125/07/20217.512.5
1002226/07/2021.10
1002227/07/2021.10
1002228/07/2021.10
1002229/07/20211012.5
1002230/07/2021.12.5
1002231/07/2021.12.5
100221/08/202112.57.5
100332/08/2021.10
100333/08/2021.10
100334/08/2021.10
100335/08/2021105
100336/08/2021.5
100337/08/2021.5
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input SubjectID	Dose 	New_dose ;
cards;
1001	.	 
1001	.	 
1001	.	 
1001	2.5	5
1001	.	 
1001	.	 
1001	.	 
1001	5	7.5
1001	.	 
1001	.	 
1001	7.5	12.5
1002	.	 
1002	.	 
1002	.	 
1002	10	12.5
1002	.	 
1002	.	 
1002	12.5	7.5
1003	.	 
1003	.	 
1003	.	 
1003	10	5
1003	.	 
1003	.	 
;

data want;
if 0 then set have;
 do until(last.SubjectID or not missing(New_dose));
   set have;
   by SubjectID;
   retain _Dose _New_dose _lag_New_dose;
   if first.SubjectID then first=1;
   if not missing(Dose) then _Dose=Dose;
   if not missing(New_dose) then _New_dose=New_dose;
 end;

  do until(last.SubjectID or not missing(New_dose));
   set have;
   by SubjectID;
   if missing(New_dose) then do;
     if first then New_dose2=_Dose;
	  else New_dose2=_lag_New_dose;
   end;
   else New_dose2=_New_dose;
   output;
 end;
 _lag_New_dose=_New_dose;
 drop first _:;
run;
Sri_devi
Obsidian | Level 7

hello,

 

thank you for your help. but i have a question if i have still additional subjects with no dose & New_dose then i want the new_dose2 as 0 instead of lag value of previous subject data.

 

ex:

 

data have;
infile cards expandtabs truncover;
input SubjectID Dose New_dose ;
cards;
1001 .
1001 .
1001 .
1001 2.5 5
1001 .
1001 .
1001 .
1001 5 7.5
1001 .
1001 .
1001 7.5 12.5
1002 .
1002 .
1002 .
1002 10 12.5
1002 .
1002 .
1002 12.5 7.5
1003 .
1003 .
1003 .
1003 10 5
1003 .
1003 .
1004 .
1004 .
1004 .
1004 .
1004 .
1005 .
1005 .
1005 .
1005 .
1005 .
;

 

 

 

can you please help me with this.

 

 

thanks!

Ksharp
Super User

OK. Change a little in code for this .

 

data have;
infile cards expandtabs truncover;
input SubjectID	Dose 	New_dose ;
cards;
1001 .
1001 .
1001 .
1001 2.5 5
1001 .
1001 .
1001 .
1001 5 7.5
1001 .
1001 .
1001 7.5 12.5
1002 .
1002 .
1002 .
1002 10 12.5
1002 .
1002 .
1002 12.5 7.5
1003 .
1003 .
1003 .
1003 10 5
1003 .
1003 .
1004 .
1004 .
1004 .
1004 .
1004 .
1005 .
1005 .
1005 .
1005 .
1005 .
;

data want;
if 0 then set have;
 do until(last.SubjectID or not missing(New_dose));
   set have;
   by SubjectID;
   retain _Dose _New_dose _lag_New_dose;
   if first.SubjectID then do;
        first=1;_Dose=0;_New_dose=0;_lag_New_dose=0;
   end;
   if not missing(Dose) then _Dose=Dose;
   if not missing(New_dose) then _New_dose=New_dose;
 end;

  do until(last.SubjectID or not missing(New_dose));
   set have;
   by SubjectID;
   if missing(New_dose) then do;
     if first then New_dose2=_Dose;
	  else New_dose2=_lag_New_dose;
   end;
   else New_dose2=_New_dose;
   output;
 end;
 _lag_New_dose=_New_dose;
 drop first _:;
run;
Ksharp
Super User

Or you could try this one .

 

data have;
infile cards expandtabs truncover;
input SubjectID	Dose 	New_dose ;
cards;
1001 .
1001 .
1001 .
1001 2.5 5
1001 .
1001 .
1001 .
1001 5 7.5
1001 .
1001 .
1001 7.5 12.5
1002 .
1002 .
1002 .
1002 10 12.5
1002 .
1002 .
1002 12.5 7.5
1003 .
1003 .
1003 .
1003 10 5
1003 .
1003 .
1004 .
1004 .
1004 .
1004 .
1004 .
1005 .
1005 .
1005 .
1005 .
1005 .
;
data have1;
 set have(keep=SubjectID Dose rename=(Dose=_Dose));
 by SubjectID;
 if first.SubjectID then n=0;
 if not missing(_Dose) then n+1;
 if not missing(_Dose) and n=1;
 drop n;
run;

data have2;
 update have(keep=SubjectID New_dose obs=0) have(keep=SubjectID New_dose);
 by SubjectID;
 output;
 rename New_dose=New_dose2;
run;

data want;
 merge have have1 have2;
 by SubjectID;
New_dose2=coalesce(New_dose2,_Dose,0);
drop _Dose;
run;
Sri_devi
Obsidian | Level 7
Thank you for your help.
can you please help me to convert the subjectID - has data value - character format - $6 and informat $6 and lenght 6 to numeric format
subjectID
01-001
01-002
01-003

thanks!
Tom
Super User Tom
Super User

@Sri_devi wrote:
Thank you for your help.
can you please help me to convert the subjectID - has data value - character format - $6 and informat $6 and lenght 6 to numeric format
subjectID
01-001
01-002
01-003

thanks!

This looks like a new question, probably better to start a new thread for it.

What numbers should those strings become?  You have to define the rules before you can program them.

Also why would you want to convert and ID variable into a number.  You shouldn't by doing any arithmetic with ID variable values.

Ksharp
Super User

As Tom said ,start a brand new session, and post the output you would like to see .

 

proc format;
picture fmt
 low-high='99-999';
run;

data have;
input subjectID $;
num_subjid=input(compress(subjectID,,'kd'),best32.);
format num_subjid fmt.;
cards;
01-001
01-002
01-003
;
Sri_devi
Obsidian | Level 7

Thank you for your help.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 1472 views
  • 5 likes
  • 4 in conversation