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

Hi,

 

A SAS rookie needs help in creating a new variable base on the following rule.

 

1. If a or b is not missing at any visits, then d is equal to the last non-missing values between a and b whichever is greater.

2. if a and b are missing at all visits then d is equal to the last non-missing value of c.

 

For example, Data like is 

IDabvisitc
1 1.2563.255
1  7 
13.00 88.515
1  9 
2  61.512
2  76.608
2  8 
2  913.381
3  62.729
3  75.951
3  88.515
3  911.737
40.251.0063.321
4  76.97
4  88.515
4  913.545

 

Data want is 

 

IDabvisitcd
13.00 88.5153.00
2  913.38113.381
3  911.73711.737
40.251.0063.3211.00
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data have;
infile cards truncover expandtabs;
input ID	a	b	visit	c;
cards;
1	.	1.25	6	3.255
1	.	.	7	.
1	3	.	8	8.515
1	.	.	9	
2	.	.	6	1.512
2	.	.	7	6.608
2	.	.	8	
2	.	.	9	13.381
3	.	.	6	2.729
3	.	.	7	5.951
3	.	.	8	8.515
3	.	.	9	11.737
4	0.25	1	6	3.321
4	.	.	7	6.97
4	.	.	8	8.515
4	.	.	9	13.545
;

data want;
 do i=1 by 1 until(last.id);
  set have;
  by id;
  if not missing(a) or not missing(b) then idx=i;
 end;
 if missing(idx) then do;flag=1;idx=i;end;
 do j=1 by 1 until(last.id);
  set have;
  by id;
  if j<=idx and not missing(c) then do; 
   _a=a;_b=b;_visit=visit;_c=c;
   if flag then d=c;
     else d=coalesce(b,a);
  end;
 end;
 keep id _: d;
 run;
 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @CHL0320  If I understand you correctly, it's pretty straight forward double DOW

 



data have;
infile cards truncover;
input ID	a	b	visit	c;
cards;
1	.	1.25	6	3.255
1	.	.	7	.
1	3	.	8	8.515
1	.	.	9	
2	.	.	6	1.512
2	.	.	7	6.608
2	.	.	8	
2	.	.	9	13.381
3	.	.	6	2.729
3	.	.	7	5.951
3	.	.	8	8.515
3	.	.	9	11.737
4	0.25	1	6	3.321
4	.	.	7	6.97
4	.	.	8	8.515
4	.	.	9	13.545
;

data want;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  t=max(a,b);
  if t then do; t1=visit;t2=t;end;
  if c then do; c1=c;c2=visit;end;
 end;
 do _n_=1 to _n_;
  set have;
  if t2 then if t1=visit then do;
   d=t2;
   output;
  end;
  if t2=. and c2=visit then do;
   d=c1;
   output;
  end;
 end;
 drop c1 c2 t:;
run;
Ksharp
Super User

data have;
infile cards truncover expandtabs;
input ID	a	b	visit	c;
cards;
1	.	1.25	6	3.255
1	.	.	7	.
1	3	.	8	8.515
1	.	.	9	
2	.	.	6	1.512
2	.	.	7	6.608
2	.	.	8	
2	.	.	9	13.381
3	.	.	6	2.729
3	.	.	7	5.951
3	.	.	8	8.515
3	.	.	9	11.737
4	0.25	1	6	3.321
4	.	.	7	6.97
4	.	.	8	8.515
4	.	.	9	13.545
;

data want;
 do i=1 by 1 until(last.id);
  set have;
  by id;
  if not missing(a) or not missing(b) then idx=i;
 end;
 if missing(idx) then do;flag=1;idx=i;end;
 do j=1 by 1 until(last.id);
  set have;
  by id;
  if j<=idx and not missing(c) then do; 
   _a=a;_b=b;_visit=visit;_c=c;
   if flag then d=c;
     else d=coalesce(b,a);
  end;
 end;
 keep id _: d;
 run;
 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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