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;
Person | Color |
Paul | green |
Paul | blue |
Paul | red |
Paul | green |
Paul | blue |
end result
Person | Color | Count |
Paul | blue | 1 |
Paul | green | 0 |
Paul | red | 0 |
Paul | green | 0 |
Paul | blue | 1 |
Trying to understand if there was a volume column there as well .. to sum up that column?
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.
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..
So:
proc sql; create table WANT as select distinct COLOR, count(COLOR) as NUM from HAVE group by COLOR; quit;
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.
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?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.