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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1405 views
  • 0 likes
  • 3 in conversation