My data is like:
Id | year | month | response |
100131 | 2014 | 3 | 0 |
100131 | 2014 | 3 | 1 |
100131 | 2014 | 4 | 0 |
100131 | 2014 | 4 | 0 |
100131 | 2014 | 5 | 0 |
100131 | 2014 | 5 | 0 |
100131 | 2014 | 5 | 0 |
100131 | 2014 | 5 | 0 |
100131 | 2014 | 5 | 0 |
100131 | 2014 | 5 | 0 |
100131 | 2014 | 5 | 1 |
100131 | 2014 | 5 | 1 |
100131 | 2014 | 5 | 1 |
100131 | 2014 | 5 | 1 |
100131 | 2014 | 5 | 1 |
I want to count the number of 0 response and number of 1 response for each month. i have sorted the data by id year month and response.
the output must be the following:
id | year | month | response | count |
100131 | 2014 | 3 | 0 | 1 |
100131 | 2014 | 3 | 1 | 1 |
100131 | 2014 | 4 | 0 | 2 |
100131 | 2014 | 5 | 0 | 6 |
100131 | 2014 | 5 | 1 | 5 |
the code i have used is :
data step......
......
...
if first.month and first.response then count=0;
count+1;
if last.response then output;
but i am not getting the expected output..
please help me in fixing this.
thanks in advance..
Where is variable RESULT?
Without a variable RESULT in a BY statement there can be no FIRST.RESULT or LAST.RESULT
sorry it is the 'response' I have unknowingly written as result. may be the code goes like this;
if first.month and first.response then count=0;
count+1;
if last.response then output;
Leave off FIRST.MONTH from the first IF.
the problem with my data is that, not all the id will have 0 and 1 some times the value is only 0 for all the month, so during that time the 0 will be counted despite the month change. but what I want is the response count for each month when it is 0 and 1.. so i cannot omit the first.month
proc freq data=have;
tables id*year*month*response/ list nopercent nocum;
run;
if you want a dataset as well add OUT=newdatasetname after the / above.
I got the same result in proc freq using similar code like urs. but I wanted to try in datastep if possible.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.