Hello!
I have panel data with id as panel variable and date as time variable. I want to create identifiers of groups of respondents based on the first value that each respondent has on the variable confidence. I use Stata 17.
Below is an excerpt with the relevant variables.
clear
input long id float(date confidence)
1 723 7
1 724 .
1 725 .
1 726 .
1 727 .
1 728 .
1 729 .
1 730 .
1 731 .
1 732 .
1 733 .
1 734 .
1 735 .
1 736 .
1 737 .
1 738 .
1 739 .
1 740 .
1 741 .
1 742 .
1 743 .
1 744 .
1 745 .
1 746 .
1 747 .
1 748 .
1 749 .
1 750 .
1 751 .
1 752 .
5 723 7
5 724 7
5 725 6
5 726 6
5 727 7
5 728 7
5 729 8
5 730 8
5 731 8
5 732 8
5 733 8
5 734 8
5 735 8
5 736 8
5 737 8
5 738 7
5 739 8
5 740 8
5 741 7
5 742 7
5 743 7
5 744 7
5 745 7
5 746 8
5 747 8
5 748 8
5 749 8
5 750 8
5 751 8
5 752 8
11 741 5
11 742 .
11 743 .
11 744 .
11 745 .
11 746 .
11 747 .
11 748 .
11 749 .
11 750 .
11 751 .
11 752 .
end
format %tmMon-YY date
[/CODE]
So far, I have the following code:
by id (date): gen byte start1 = confidence[_n==1]==1
levelsof id if start1 = 1, local(unqID1)
gen byte groupidentifier1 = 0
foreach 1 of local unqID1 {
replace groupidentifier1 = 1
}
Unfortunately, the foreach command results in all values of the variable groupidentifier1 becoming 1, although I want this to be only the case for the subset of respondents in the local unqID1.
For further background, what I ultimately want to do is create time-series on confidence for each group of individuals that start out with a specific value on confidence. I suppose that would be bysort date: egen journey1 = mean(confidence) if groupidentifier1 == 1 and so forth.
Can somebody help me? Maybe also with a more efficient way?
I have panel data with id as panel variable and date as time variable. I want to create identifiers of groups of respondents based on the first value that each respondent has on the variable confidence. I use Stata 17.
Below is an excerpt with the relevant variables.
clear
input long id float(date confidence)
1 723 7
1 724 .
1 725 .
1 726 .
1 727 .
1 728 .
1 729 .
1 730 .
1 731 .
1 732 .
1 733 .
1 734 .
1 735 .
1 736 .
1 737 .
1 738 .
1 739 .
1 740 .
1 741 .
1 742 .
1 743 .
1 744 .
1 745 .
1 746 .
1 747 .
1 748 .
1 749 .
1 750 .
1 751 .
1 752 .
5 723 7
5 724 7
5 725 6
5 726 6
5 727 7
5 728 7
5 729 8
5 730 8
5 731 8
5 732 8
5 733 8
5 734 8
5 735 8
5 736 8
5 737 8
5 738 7
5 739 8
5 740 8
5 741 7
5 742 7
5 743 7
5 744 7
5 745 7
5 746 8
5 747 8
5 748 8
5 749 8
5 750 8
5 751 8
5 752 8
11 741 5
11 742 .
11 743 .
11 744 .
11 745 .
11 746 .
11 747 .
11 748 .
11 749 .
11 750 .
11 751 .
11 752 .
end
format %tmMon-YY date
[/CODE]
So far, I have the following code:
by id (date): gen byte start1 = confidence[_n==1]==1
levelsof id if start1 = 1, local(unqID1)
gen byte groupidentifier1 = 0
foreach 1 of local unqID1 {
replace groupidentifier1 = 1
}
Unfortunately, the foreach command results in all values of the variable groupidentifier1 becoming 1, although I want this to be only the case for the subset of respondents in the local unqID1.
For further background, what I ultimately want to do is create time-series on confidence for each group of individuals that start out with a specific value on confidence. I suppose that would be bysort date: egen journey1 = mean(confidence) if groupidentifier1 == 1 and so forth.
Can somebody help me? Maybe also with a more efficient way?
Comment