Help using Base SAS procedures

Retain Flag=1 with ID number

Reply
Contributor
Posts: 62

Retain Flag=1 with ID number

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Retain Flag=1 with ID number

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.
Contributor
Posts: 62

Re: Retain Flag=1 with ID number

Thanks for your response!
N/A
Posts: 0

Re: Retain Flag=1 with ID number

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;
Contributor
Posts: 62

Re: Retain Flag=1 with ID number

Well perfect, this works great!! Thanks!! (got to renew my courses...)
Contributor
Posts: 43

Re: Retain Flag=1 with ID number

Why can't you try this

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

hope i understood u r qns properly
N/A
Posts: 0

Re: Retain Flag=1 with ID number

Because you want to propagate a flag value of 0 if the first record (for the given ID) has a flag of 0.
Ask a Question
Discussion stats
  • 6 replies
  • 149 views
  • 0 likes
  • 4 in conversation