BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Wafflecakes
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Add to your knowledge base: sorting the data is not enough to create first. and last. variables in a DATA step. You also must add a BY statement to the DATA step.

This should work;

Proc sort data=have;
By ID numberofcars;
Run;

Data want;
Set have,
By ID numberofcars;
If first.numberofcars and
Numberofcars > 1 then flag=1;
Run;

View solution in original post

6 REPLIES 6
ScottBass
Rhodochrosite | Level 12

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?


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Wafflecakes
Calcite | Level 5
Hi Scott. I edited my original post. Does it make more sense? I don't know what the "want" data steps should look like...
ScottBass
Rhodochrosite | Level 12

@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 🙂


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Astounding
PROC Star

@ScottBass ,

 

You've always provided good and thoughtful answers.  It's a pleasure to see you on the board, as time allows.

 

 

Astounding
PROC Star
Add to your knowledge base: sorting the data is not enough to create first. and last. variables in a DATA step. You also must add a BY statement to the DATA step.

This should work;

Proc sort data=have;
By ID numberofcars;
Run;

Data want;
Set have,
By ID numberofcars;
If first.numberofcars and
Numberofcars > 1 then flag=1;
Run;
Wafflecakes
Calcite | Level 5
Ahhh sorry. I forgot to write that in the original post. Still, it works now! Thanks Astounding.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 5298 views
  • 0 likes
  • 3 in conversation