BookmarkSubscribeRSS Feed
Wouter
Obsidian | Level 7
I learned it a year ago at my SAS Base course, but right now, I don't remember anymore... If anyone could help me?

I've got a dataset with ID numbers, accompanied by dates (so several ID numbers are the same). Only the first of some ID numbers is flagged with FLAG=1. Now I want all the flagged numbers to have a FLAG=1 for all of them. See below a sample:

ID Date Flag
1 01-01-2008 1
1 03-01-2008 0
1 05-01-2008 0
2 06-04-2008 0
2 10-04-2008 0
3 01-01-2008 1
3 01-02-2008 0
3 01-08-2008 0
4 01-01-2008 0
5 11-01-2008 0
5 12-01-2008 0

So I want for ID 1 and 3 a Flag=1 for all their presences.

I tried something like:

if first.id then do;
retain FLAG;
if last.id then do;
FLAG=1;end;end;

But this doesn't work. Anyone an idea? Thanks in advance!

Message was edited by: Wouter Message was edited by: Wouter
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The RETAIN statement need not be coded within the DO/END, it is interpreted at compilation. Also, if you are missing a BY statement, then you would see pertinent diagnostic messages in your SAS log about UNINITIALIZED. You will want reset the variable back to 0 or a SAS missing_value, I expect with each new FIRST.ID condition, correct?

Suggest you add:

PUTLOG _ALL_;

to help debug your DATA step and the program.

Scott Barry
SBBWorks, Inc.
Wouter
Obsidian | Level 7
Thanks for your response!
deleted_user
Not applicable
I believe you'll need to create an auxiliary variable for the RETAIN statement, which you can just drop.

Try something like this:

data TEST;

retain tmp_flag;

set test; by id;

if first.id then do;
tmp_flag = flag;
end;

flag = tmp_flag;

drop tmp_flag;
run;
Wouter
Obsidian | Level 7
Well perfect, this works great!! Thanks!! (got to renew my courses...)
ssas
Calcite | Level 5
Why can't you try this

data test1;
set test;
if id ne " " then flag=1;
run;

hope i understood u r qns properly
deleted_user
Not applicable
Because you want to propagate a flag value of 0 if the first record (for the given ID) has a flag of 0.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1571 views
  • 0 likes
  • 4 in conversation