program create_inp ! ! CREATE_INP creates an input file used by read_scf.f90, a Fortran ! program that extracts variables of interest from the ! Survey of Consumer Finances datasets (1989-2004). ! Create_inp calls varsofint.inp which is a list of SCF ! variables of interest to the user. The first line ! is the number of variables to read in. This line is ! followed by lines with the SCF variables of interest ! (and, optionally, a brief description of the variable). ! An example of the file varsofint.inp is: ! 3 ! X42001 Revised weights ! X8022 Age (respondent) ! X8021 Sex (respondent) ! ! For Create_inp to work properly, users need to make sure ! that the SCF variable name is in columns 1-6 of the input ! file varsofint.inp, followed by a space. ! ! Users should also edit the (Year)map.txt file provided ! by the SCF (which is read by this program) and make ! sure that variable names are in columns 1-6. In some ! cases, the line of (Year)map.txt starts with a ". If so, ! edit the file and do a global find and replace to eliminate ! the quotes. (For UNIX users, it may also be necessary ! to create a new map.txt file and load in SCF file. The ! end-of-line characters may confuse Create_inp.) ! ! Create_inp also assumes that the last thing on each line ! of (Year)map.txt indicates the column position of the ! variable in the SCF database (e.g., scf(Year).ascii). ! ! Users need to edit the open statements to put in the ! relevant map file before beginning. ! Ellen McGrattan, 7-27-06 ! Revised, ERM, 2-2-07 implicit none integer :: i,nv,io_stat,lv,lstr,ld,ifound,j,k character (len=7) :: vint,vstr character (len=100) :: str,desc open(unit=5, file='varsofint.inp') open(unit=7, file='/media/IOMEGA_HDD/SCFdata/1995Survey/95map.txt') open(unit=8, file='v2extract95.inp') ! ! Find the lines of the map file that contain variables of interest ! read(5,*) nv write(8,'(i4)') nv do i=1,nv ! ! Read in a new variable of interest (vint) and a description (desc) ! read(5,'(a)') str vint = ' ' lv = len_trim(str(1:7)) vint(1:lv) = str(1:lv) ld = len_trim(str)-7 desc(1:ld) = str(8:ld+7) ! ! Read through the SCF (year)map.txt file to find it ! rewind(7) io_stat = 0 ifound = 0 do while ((io_stat>=0).and.(ifound==0)) read(7,'(a)',iostat=io_stat) str vstr = ' ' vstr(1:lv) = str(1:lv) j = char_to_int(str(lv+1:lv+1)) if ((io_stat>=0).and.(vstr(1:lv)==vint(1:lv)).and.(j==-1)) ifound = 1 enddo ! ! Alert the user if variable not found in the SCF file ! if (ifound==0) then write(*,'(a7,a14)') vint,' was not found' write(8,'(i5,1x,i2,1x,a7,a)') -1,-1,vint,desc(1:ld) else lstr = len_trim(str) j = char_to_int(str(lstr-4:lstr)) if (j==-1) j = char_to_int(str(lstr-3:lstr)) if (j==-1) j = char_to_int(str(lstr-2:lstr)) if (j==-1) j = char_to_int(str(lstr-1:lstr)) if (j==-1) j = char_to_int(str(lstr:lstr)) if (vint(1:1)=='J') then k = 6 else k = 14 endif j = j-k+1 ! ! Write out results to v2extract.inp ! write(8,'(i5,1x,i2,1x,a7,a)') j,k,vint,desc(1:ld) endif enddo contains function char_to_int (string) result (number) ! ! Convert a character string to an integer. ! Set the integer to -1 if the string contains anything other than [0-9]. ! integer :: number character (len=*) :: string if ((len(string)>0).and.(verify(string,"0123456789")==0)) then read (string, *) number else number = -1 endif end function char_to_int end program create_inp