BookmarkSubscribeRSS Feed
CynthiaWei
Obsidian | Level 7
Hi SAS Pros,
 
I want to create a new variable---Apple_Count_by_Name. For example, each day the teach gives students a different type of fruit. If a student receives an apple, then it is a Yes=1, if not, then is No=0. I need to know how many apples a student has from 1/2 to 1/9. Please ignore the absence dates for some students.
 
What I have"
NameDateFruitIs_it_an_apple
Tom2-Janapple1
Tom3-Janapple1
Tom4-Janorange0
Tom5-Janpear0
Tom6-Janpeach0
Tom7-Janblueberry0
Tom8-Janblueberry0
Tom9-Janapple1
Mike2-Janorange0
Mike3-Janpear0
Mike4-Janapple1
Mike5-Janapple1
Mike6-Janblueberry0
Megan2-Janapple1
Megan3-Janorange0
Megan4-Janpear0
Lily2-Janapple1
Lily3-Janapple1
Lily4-Janblueberry0
Lily5-Janapple1
Lily6-Janorange0
Lily7-Janpear0

 

What I want

NameDateFruitIs_it_an_appleApple_Count_by_Name
Tom2-Janapple13
Tom3-Janapple13
Tom4-Janorange03
Tom5-Janpear03
Tom6-Janpeach03
Tom7-Janblueberry03
Tom8-Janblueberry03
Tom9-Janapple13
Mike2-Janorange02
Mike3-Janpear02
Mike4-Janapple12
Mike5-Janapple12
Mike6-Janblueberry02
Megan2-Janapple11
Megan3-Janorange01
Megan4-Janpear01
Lily2-Janapple13
Lily3-Janapple13
Lily4-Janblueberry03
Lily5-Janapple13
Lily6-Janorange03
Lily7-Janpear03

 

Thank you in advance for any help with this question!

 

All the best,

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20
data have;
input Name $	Date $	Fruit $	Is_it_an_apple;
datalines;
Tom	2-Jan	apple	1
Tom	3-Jan	apple	1
Tom	4-Jan	orange	0
Tom	5-Jan	pear	0
Tom	6-Jan	peach	0
Tom	7-Jan	blueberry	0
Tom	8-Jan	blueberry	0
Tom	9-Jan	apple	1
Mike	2-Jan	orange	0
Mike	3-Jan	pear	0
Mike	4-Jan	apple	1
Mike	5-Jan	apple	1
Mike	6-Jan	blueberry	0
Megan	2-Jan	apple	1
Megan	3-Jan	orange	0
Megan	4-Jan	pear	0
Lily	2-Jan	apple	1
Lily	3-Jan	apple	1
Lily	4-Jan	blueberry	0
Lily	5-Jan	apple	1
Lily	6-Jan	orange	0
Lily	7-Jan	pear	0
;

proc sql;
   create table want as
   select *,
          sum(Is_it_an_apple)
   from have
   group by name;
quit;
CynthiaWei
Obsidian | Level 7

I appreciate your response very much!

 

But, how to create a variable named Apple_Count_by_Name, a variable name that I want, in the step that you just told me.

 

Best,

 

 

PeterClemmensen
Tourmaline | Level 20

Like this

 

proc sql;
   create table want as
   select *,
          sum(Is_it_an_apple) as Apple_Count_by_Name
   from have
   group by name;
quit;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is exactly the same question as from:

https://communities.sas.com/t5/SAS-Programming/How-to-count-actual-counts-in-a-sequence-by-ID/m-p/54...

 

For which you will get exactly the same answers, a simple change from count() to sum() is what you are looking for.

PeterClemmensen
Tourmaline | Level 20

Alternatively, you can do this. This preserves the original order in the data set

 

data want;

   do until (last.name);
      set have;
      by Name notsorted;
      Apple_Count_by_Name=sum(Apple_Count_by_Name, Is_it_an_apple);
   end; 
 
   do until (last.Name);
      set have;
      by Name notsorted;
      output;
   end;

run;
CynthiaWei
Obsidian | Level 7
Thank you very much! May I know what is "last" in the do until statement? And, there is a "." in between last and name?

Best,
ballardw
Super User

@CynthiaWei wrote:
Thank you very much! May I know what is "last" in the do until statement? And, there is a "." in between last and name?

Best,

When a BY statement is used in a data step SAS creates temporary variables to indicate that a specific value is the first or last of the by group variable. So FIRST.variable or LAST.variable is a true/false value that is true (or 1) when the observation is the first one with that value. The dot indicates the use of this special feature instead of expecting or creating a variable named firstvariable or lastvariable.

 

Multiple variables on the BY statement means each variable gets its own first and last indicators.

If First.variable = Last.variable then there is only one value in the by group.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 969 views
  • 0 likes
  • 4 in conversation