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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1144 views
  • 0 likes
  • 4 in conversation