Dear experts,
Could I request you to help me to solve my problem.
Input dataset
ID | test | base1 | baseflag |
1 | Liver | 50 | Y |
1 | Liver | ||
1 | Liver | ||
1 | Lung | ||
1 | Lung | 80 | Y |
1 | Lung | ||
2 | wight | ||
2 | wight | ||
2 | wight | 70 | Y |
2 | wight | ||
2 | wight |
I need to retain basline flag values within the id and test.
Output required
ID | test | base1 | baseflag | base |
1 | Liver | 50 | Y | 50 |
1 | Liver | 50 | ||
1 | Liver | 50 | ||
1 | Lung | |||
1 | Lung | 80 | Y | 80 |
1 | Lung | 80 | ||
2 | wight | |||
2 | wight | |||
2 | wight | 70 | Y | 70 |
2 | wight | 70 | ||
2 | wight | 70 |
I tried with below code. But it did not work.
data test1;
set test;
by id test;
retain BASE_;
if not missing(base) then Base_=base;
else Base=Base_;
run;
Please suggest.
Thanks,
Rjy9
Try this
data have;
input ID test $ base1 baseflag $;
infile datalines missover;
datalines;
1 Liver 50 Y
1 Liver
1 Liver
1 Lung
1 Lung 80 Y
1 Lung
2 wight
2 wight
2 wight 70 Y
2 wight
2 wight
;
data want;
set have;
by ID test;
if first.test then base = .;
if base1 then base = base1;
retain base;
run;
Try this
data have;
input ID test $ base1 baseflag $;
infile datalines missover;
datalines;
1 Liver 50 Y
1 Liver
1 Liver
1 Lung
1 Lung 80 Y
1 Lung
2 wight
2 wight
2 wight 70 Y
2 wight
2 wight
;
data want;
set have;
by ID test;
if first.test then base = .;
if base1 then base = base1;
retain base;
run;
Thank you so much for your very quick response. It solved my problem 🙂
Note that this
if base1 then base = base1;
does not work right when BASE1 is zero since SAS considers zero and missing as FALSE.
Either use
if not missing(base1) then base = base1;
Or more simply
base = coalesce(base1,base);
Thank you 🙂
You need to reset at a group change:
data test1;
set test;
by id test;
retain base;
if first.test then call missing(base);
if not missing(base1) then base = base1;
run;
Thank you so much for your very quick response. Much appreciated 🙂
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.