Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Trying to compare records in a data step

Reply
Occasional Contributor
Posts: 16

Trying to compare records in a data step

I have a table and I am trying to compare the the colors. Basically in the datastep... I want the primary color blue to count.. if the primary color blue isn't there I want to count the green... if no green then don't count. I am trying to understand how to write this so the observation looks at the next one

 

data new; set color_table;

if first.person and color='green' then;

count;

end;

 

PersonColor
Paulgreen
Paulblue
Paulred
Paulgreen
Paulblue

 

end result

 

PersonColorCount
Paulblue1
Paulgreen0
Paulred0
Paulgreen0
Paulblue1

 

Occasional Contributor
Posts: 16

Re: Trying to compare records in a data step

Trying to understand if there was a volume column there as well .. to sum up that column?

Super User
Super User
Posts: 7,392

Re: Trying to compare records in a data step

Sorry, I am really not following the logic.  What do you mean by if blue is not there then count green?  Both colors are in the data?  If you mean sequentially, then you would need to attribute a distinct sequential order to the data.

Occasional Contributor
Posts: 16

Re: Trying to compare records in a data step

what I am trying  to do is count one color or the other... For Paul.. I want to add 1 if the color is blue.. but if the next record is green.. don't count green..

Super User
Super User
Posts: 7,392

Re: Trying to compare records in a data step

So:

proc sql;
  create table WANT as
  select   distinct COLOR,
              count(COLOR) as NUM
  from     HAVE
  group by COLOR;
quit;
Occasional Contributor
Posts: 16

Re: Trying to compare records in a data step

This will give me a count by group.. however I only want to count green if blue isn't there.
Super User
Super User
Posts: 7,392

Re: Trying to compare records in a data step

Which then goes back to my previous question as in your test data, blue is clearly there?

Person Color
Paul green
Paul blue
Paul red
Paul green
Paul blue

 

Second and last row.

Occasional Contributor
Posts: 16

Re: Trying to compare records in a data step

Yep.. I know .. but I have a large data set.. and it won't always be the case. I need to look at each observation to see what the color is.. then if the first oberservation is blue then =1 .. if we go to the second oberservation it's green, how do I tell it to look at the previous observation to know that it was blue.
Super User
Super User
Posts: 7,392

Re: Trying to compare records in a data step

But what does observation order have to do with anything?  If there is a record in the data for that particular group of blue, then there is a records in that particular group = blue, if its the first obs or the 5th doesn't matter.  A mere matter of sorting the data appropriately will put the blue in the correct order, so I sort your dataset:

Paul   Blue

Paul   Blue

Paul   Green

Paul   Green

Paul   Red

 

There are Blue records?

Occasional Contributor
Posts: 16

Re: Trying to compare records in a data step

This is how I want the output to look at the previous obs to see if blue was in the list.. if it's not.. I want to have green have a count.. here is 2 scenarios
Person Color Count
Paul blue 1
Paul blue 1
Paul green 0
Paul green 0
Paul red 0


Person Color Count
Paul green 1
Paul green 1
Paul red 0
Ask a Question
Discussion stats
  • 9 replies
  • 373 views
  • 0 likes
  • 2 in conversation