Hello. I would like to flag all first occurrences of a value in Numberofcars when Numberofcars is >1 by participant id. I have data that looks like this:
ID Numberofcars WantFlag HaveFlag
1 1 . .
1 1 . .
1 2 1 1
1 3 1 1
1 3 . 1
2 1 . .
2 2 1 1
2 2 . 1
2 3 1 1
I wrote this code:
proc sort data=have out=want;
by ID Numberofcars;
run;
data want;
set want;
retain test;
if first.ID & first.Numberofcars then test=0;
haveflag=0;
if Numberofcars>1 & test=0 then do;
haveflag=1;
end;
run;
As you can see in haveflag (which is generated from the above code), it equals 1 for ALL occurrences of Numberofcars=3. I only want it to do this for the first one and any other unique values of Numberofcars that does not equal 1.
I'm still unsure what you want? Can you post "have" and "want" data steps, using data lines so we can just c&p and run it?
@ScottBass wrote:I'm still unsure what you want? Can you post "have" and "want" data steps, using data lines so we can just c&p and run it?
You've got your answer, but to explain my post...
If I cut-and-paste this into SAS:
ID Numberofcars WantFlag HaveFlag
1 1 . .
1 1 . .
1 2 1 1
1 3 1 1
1 3 . 1
2 1 . .
2 2 1 1
2 2 . 1
2 3 1 1
I get an error.
If I cut-and-paste this into SAS, it works:
data have;
length ID Numberofcars WantFlag HaveFlag 8;
input ID Numberofcars WantFlag HaveFlag 8;
datalines;
1 1 . .
1 1 . .
1 2 1 1
1 3 1 1
1 3 . 1
2 1 . .
2 2 1 1
2 2 . 1
2 3 1 1
;
run;
Same for "want" - use datalines to show me what you want as your desired output.
Put your SAS code in a SAS block - click the "running man" icon in the Rich Text editor.
Make it easy for me to help you. It's a slow day otherwise I wouldn't have bothered...
I'm glad Astounding figured out what you wanted. He's smarter than me 🙂
You've always provided good and thoughtful answers. It's a pleasure to see you on the board, as time allows.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.