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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.