-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwritestata1.inc
104 lines (83 loc) · 3.24 KB
/
writestata1.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
! This file is part of STATAMOD. Copyright (c) 2006-2016 Andrew Shephard
!
! STATAMOD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! STATAMOD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with STATAMOD. If not, see <http://www.gnu.org/licenses/>.
character(*), intent(in) :: thisName
character(*), intent(in), optional :: thisLabel
integer(i4) :: i, ios
integer(i1) :: tempInt1
character(33) :: tempStr33
if (.not. stataFile%saveInit) then
call statamodError('a file has not been specified to write to')
end if
if (len(thisName)>32) then
call statamodError('variable '//thisName//' exceeds 32 characters')
end if
write (TempStr33,*) stataFile%saveNObs
if (size(thisVar) /= stataFile%saveNObs) then
call statamodError('variable ' // trim(thisName) // ' is the wrong dimension. '// &
& 'Expecting an array of size ' // trim(adjustl(TempStr33)) // '*1')
end if
if (.not. stataFile%saveOnce) then
! first var to be written
allocate(stataFile%saveCurr, STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
!close(stataFile%saveUnit)
end if
if (associated(stataFile%saveCurr%next)) then
nullify(stataFile%saveCurr%next)
end if
stataFile%saveHead => stataFile%saveCurr
stataFile%saveOnce = .true.
end if
if (stataFile%saveNVar >= 32767) then
call statamodError('attempt to exceed Stata maxvar limit (32767)')
!close(stataFile%saveUnit)
end if
! var label
if (present(thisLabel)) then
tempInt1 = len(thisLabel) + 1
stataFile%saveCurr%label = thisLabel
stataFile%saveCurr%label(tempInt1:81) = char(0)
else
stataFile%saveCurr%label = char(0)
end if
! var name, first check whether it is stata legal
do i = 1, len(trim(thisName))
select case(ichar(thisName(i:i)))
case(48:57, 65:90, 95, 97:122) !0:9,a:z,_,A:Z
case default
call statamodError('variable name ' // thisName // ' contains an illegal character. ' // &
& 'Only alpha-numeric characters and underscore are allowed')
end select
end do !I
select case(ichar(thisName(1:1)))
case(48:57) !0:9
call statamodError('variable name ' // thisName // ' contains an illegal character. ' // &
& 'Names can not begin with a numeric character')
case default
end select
! now store in memory
tempInt1 = len(trim(thisName)) + 1
stataFile%saveCurr%name = trim(thisName)
stataFile%saveCurr%name(tempInt1:33) = char(0)
! and now, check unique
tempStr33 = stataFile%saveCurr%name
stataFile%saveCurr => stataFile%saveHead
do while(associated(stataFile%saveCurr%next))
if (tempStr33 == stataFile%saveCurr%name) then
call statamodError('a variable already exists with name ' // trim(tempStr33))
end if
stataFile%saveCurr => stataFile%saveCurr%next
end do