The SAS Output Delivery System and reporting techniques

first.variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

first.variables

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


Accepted Solutions
Solution
‎01-30-2015 12:11 PM
Respected Advisor
Posts: 3,799

Re: first.variables


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


All Replies
Respected Advisor
Posts: 3,799

Re: first.variables

Where is variable RESULT?

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

Occasional Contributor
Posts: 12

Re: first.variables

Posted in reply to data_null__

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;

Respected Advisor
Posts: 3,799

Re: first.variables

Leave off FIRST.MONTH from the first IF.

Occasional Contributor
Posts: 12

Re: first.variables

Posted in reply to data_null__

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

Solution
‎01-30-2015 12:11 PM
Respected Advisor
Posts: 3,799

Re: first.variables


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;
Super User
Posts: 11,343

Re: first.variables

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.

Occasional Contributor
Posts: 12

Re: first.variables

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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