BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pkmkart
Calcite | Level 5

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:

idyearmonthresponsecount
1001312014301
1001312014311
1001312014402
1001312014506
1001312014515

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..

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

data response;
   input  id year month response;
   cards;
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
;;;;
   run;
proc print;
  
run;
data counts;
   set response;
   by id month;
   array c[0:1] _temporary_;
  
if first.month then do; c[0]=0; c[1]=0; end;
   c[response]+
1;
  
if last.month then do;
     
do response=0,1;
         count = c[response];
        
output;
        
end;
     
end;
  
run;
proc print;
  
run;

View solution in original post

7 REPLIES 7
data_null__
Jade | Level 19

Where is variable RESULT?

Without a variable RESULT in a BY statement there can be no FIRST.RESULT or LAST.RESULT

pkmkart
Calcite | Level 5

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;

data_null__
Jade | Level 19

Leave off FIRST.MONTH from the first IF.

pkmkart
Calcite | Level 5

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

data_null__
Jade | Level 19

data response;
   input  id year month response;
   cards;
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
;;;;
   run;
proc print;
  
run;
data counts;
   set response;
   by id month;
   array c[0:1] _temporary_;
  
if first.month then do; c[0]=0; c[1]=0; end;
   c[response]+
1;
  
if last.month then do;
     
do response=0,1;
         count = c[response];
        
output;
        
end;
     
end;
  
run;
proc print;
  
run;
ballardw
Super User

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.

pkmkart
Calcite | Level 5

I got the same result in proc freq using similar code like urs. but I wanted to try in datastep if possible.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 2272 views
  • 0 likes
  • 3 in conversation