BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Epi_Stats
Obsidian | Level 7

Hi,

 

I have a large dataset with a summary of each respondent’s favourite fruit combination at different times of the day (breakfast, lunch, dinner):

 

 

data have;
input ID time $ fruit :$30.;
datalines; 
1 bfast apple-orange
1 bfast apple-orange
1 dinner apple-orange
2 bfast pear-peach
2 dinner pear-peach
3 bfast banana-mango
3 lunch banana-mango
3 lunch banana-mango
4 lunch kiwi-guava
4 dinner kiwi-guava
;
run;

 

 

I want to create a new variable “count” based on each fruit combination – I include an example output below of what I want. This variable does NOT need to be based on the unique ID – for example, apple-orange would have a count range 1-4, even though ID 1 and 2 share this combination.

 

data want;
input ID time $ fruit :$30. count;
datalines; 
1 bfast apple-orange 1
1 bfast apple-orange 2
1 dinner apple-orange 3
2 bfast apple-orange 4
2 dinner pear-peach 1
3 bfast banana-mango 1
3 lunch banana-mango 2
3 lunch banana-mango 3
4 lunch kiwi-guava 1
4 dinner kiwi-guava 2
;
run;

 

I thought I could use a do loop to achieve this, but I’m struggling to think how to do this in SAS, and would be very grateful for any suggestions/help!

 

Thank you in advance,

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I am confused by your problem statement. I don't see how apple-orange gets a "count range" of 1 to 4 from the original data, when apple-orange appears 3 times. Please explain.

 

However, if I may guess, you can sort the data by the fruit and then obtain a count like this:

 

proc sort data=have;
    by fruit;
run;

data want;
    set have;
    by fruit;
    if first.fruit then count=0;
    count+1;
run;

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I am confused by your problem statement. I don't see how apple-orange gets a "count range" of 1 to 4 from the original data, when apple-orange appears 3 times. Please explain.

 

However, if I may guess, you can sort the data by the fruit and then obtain a count like this:

 

proc sort data=have;
    by fruit;
run;

data want;
    set have;
    by fruit;
    if first.fruit then count=0;
    count+1;
run;

 

--
Paige Miller
Epi_Stats
Obsidian | Level 7

Thank you, @PaigeMiller  - this is precisely what I was looking for here! Just in response to your query, since I'm not counting fruit by ID, apple-orange count range is 1-4 -- sorry, I probably could have explained this better in my original post.

 

Thank you again for your help!

PaigeMiller
Diamond | Level 26

@Epi_Stats wrote:

Thank you, @PaigeMiller  - this is precisely what I was looking for here! Just in response to your query, since I'm not counting fruit by ID, apple-orange count range is 1-4 -- sorry, I probably could have explained this better in my original post.


While I am glad to solve your problem, this explanation explains nothing. There are not 4 apple-orange in your data. When you provide a desired answer, it should be consistent with the input data you provide. Providing desired output that is consistent with the input data on future questions will help you get faster and better answers. You got lucky on this question, I guessed right as to what you wanted, but I (and others) don't always guess right.

--
Paige Miller

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 853 views
  • 1 like
  • 2 in conversation