Thanks to Art and Gene for their help, but I'm still struggling to make
this code work. I'll try another way to describe the situation by showing syntax that DOES work and syntax that DOES NOT work, and maybe someone can help me, and I'm trying to use a macro instead of vectors which may work more effectively. 1. GOOD Code. This basic code works by first checking the values in two fields of my main database (Master_spare) and then depending on the results of that process, it computes a new value for a third field. GET FILE='E:\IRS Migration Data\County Migration Files\Co_9304_Master_spare.sav'. DATASET NAME Master_IRS WINDOW=FRONT. If (TO_ST = "00" And TO_ST_ABBR = "AL") To_St_2 = "01". If (TO_ST = "00" And TO_ST_ABBR = "AK") To_St_2 = "02". Execute. 2. BAD Code. Because I'm hoping to avoid having to write a single line for each of the 50 states, and I want to learn more about how to tap the power of SPSS syntax, I've tried to use vectors, loops and now I'm trying a macro. Unfortnately, the macro below does not work, although the SPSS file does have "transformations pending" on the bottom of the screen; when I choose "run transformations" nothing happens, and my data options are set on "run transformations immediately". So I don't know what transformations are pending. I've reworked this macro many times, and it should work, but it does not. GET FILE='E:\IRS Migration Data\County Migration Files\Co_9304_Master_spare.sav'. DATASET NAME Master_IRS WINDOW=FRONT. DEFINE !No_Zeros (StateFips = !Tokens(1) /StateAbbr = !Tokens(1)) Do if (TO_ST = "00" and TO_ST_ABBR = "AL"). Compute TO_ST_2 = "01". End If. !ENDDEFINE. !No_Zeros "01" "AL". EXECUTE. Any help would be much appreciated! best, Brian |
There must be something I don't get about your query.
Someone else suggested a do repeat approach, this tested syntax works. data list fixed /oldfipstate postal(2a2). begin data 01AL 02AK 56WY 00AL 00AK 00WY END DATA. LIST. string newfipstate (a2). do if oldfipstate eq '00'. do repeat fipscode ='01','02','56' / postalcode= 'AL','AK','WY'. if postal eq postalcode newfipstate = fipscode. end repeat. else. compute newfipstate = oldfipstate. end if. list. You would only have to modify it so that it held 50 values. (aside: since it is IRS data, shouldn't you have DC, PR, AS, etc.?) The approach I suggested puts the constants in a lookup table. If you are just starting with SPSS, you should seriously consider skipping scripts and macros and going with syntax and PYTHON. Art Kendall Social Research Consultants Brian Kelley wrote: >Thanks to Art and Gene for their help, but I'm still struggling to make >this code work. I'll try another way to describe the situation by showing >syntax that DOES work and syntax that DOES NOT work, and maybe someone can >help me, and I'm trying to use a macro instead of vectors which may work >more effectively. > >1. GOOD Code. This basic code works by first checking the values in two >fields of my main database (Master_spare) and then depending on the >results of that process, it computes a new value for a third field. > >GET FILE='E:\IRS Migration Data\County Migration >Files\Co_9304_Master_spare.sav'. >DATASET NAME Master_IRS WINDOW=FRONT. > >If (TO_ST = "00" And TO_ST_ABBR = "AL") To_St_2 = "01". >If (TO_ST = "00" And TO_ST_ABBR = "AK") To_St_2 = "02". > >Execute. > >2. BAD Code. Because I'm hoping to avoid having to write a single line for >each of the 50 states, and I want to learn more about how to tap the power >of SPSS syntax, I've tried to use vectors, loops and now I'm trying a >macro. Unfortnately, the macro below does not work, although the SPSS file >does have "transformations pending" on the bottom of the screen; when I >choose "run transformations" nothing happens, and my data options are set >on "run transformations immediately". So I don't know what transformations >are pending. I've reworked this macro many times, and it should work, but >it does not. > >GET FILE='E:\IRS Migration Data\County Migration >Files\Co_9304_Master_spare.sav'. >DATASET NAME Master_IRS WINDOW=FRONT. > >DEFINE !No_Zeros (StateFips = !Tokens(1) /StateAbbr = !Tokens(1)) > Do if (TO_ST = "00" and TO_ST_ABBR = "AL"). > Compute TO_ST_2 = "01". > End If. >!ENDDEFINE. > >!No_Zeros "01" "AL". >EXECUTE. > >Any help would be much appreciated! > >best, > >Brian > > > >
Art Kendall
Social Research Consultants |
In reply to this post by Brian-31
Brian,
It seem like we have made several trips over the same ground and the helpers haven't been helfpful. Richard's question >>(If you'll excuse me, why the project in the first place? You have two perfectly good state codes already: FIPS and postal. And if you have two different 2-digit state codes in your file - FIPS, in which Alabama is "00", and yours in which Alabama is "01" - you've handed yourself great opportunity for confusion.) Is a good one. I'm curious to read what you'll say. You offered this as an example of 'good code'. If (TO_ST = "00" And TO_ST_ABBR = "AL") To_St_2 = "01". If (TO_ST = "00" And TO_ST_ABBR = "AK") To_St_2 = "02". So let's see you can help me understand something that has never been clear. 1) Is TO_ST the master file FIPS code variable? 2) Assuming that TO_ST is the master file FIPS code variable, is it possible that the value of TO_ST_ABBR for the TO_ST value is incorrect given a table of the true abbreviation associated with each FIPS code? 3) If 2) is true, then is the object of this job to mark records having the incorrect abbreviation for the listed FIPS code? Please respond to each question individually and an answer is No, please indicate the true statement. Gene Maguin |
In reply to this post by Brian-31
At 11:35 AM 11/22/2006, Brian Kelley wrote:
>1. GOOD Code. This is a perfectly workable data-in-code solution, and might still be the least trouble. >If (TO_ST = "00" And TO_ST_ABBR = "AL") To_St_2 = "01". >If (TO_ST = "00" And TO_ST_ABBR = "AK") To_St_2 = "02". A variation, that I didn't think of before, is to use this solution, but generate the code automatically, then INCLUDE it. Gene Maguin wrote, >The way to do this is to create a new file with two columns: one is >the FIPS code, call this variable CORRECTFIPS and the other is the >state abbreviation, which must be called TO_ST_ABBR. That is, look up the FIPS code from the state abbreviation (which I'll keep calling "postal code"). Gene's logic, which is correct, is >Get your master file and sort it by TO_ST_ABBR. Then do a match files >using the table option to add the correct fips code variable >(CORRECTFIPS) Here's the variation, which generates the RECODE automatically from the input. The code generator is tested, but the code it generates isn't. This is SPSS draft output, except the section "Include the generated code as a lookup table", which is pasted in without being executed. * Test data, as posted . Data List /State_FIPS2 1-2 (A) State_Abbr2 3-4 (A). Data List will read 1 records from the command file Variable Rec Start End Format State_FIPS2 1 1 2 A2 State_Abbr2 1 3 4 A2 Begin Data 01AL 02AK 04AZ 05AR 06CA 08CO End Data. * ................................................... . * Modest kludge, so the code generator can recognize . * the beginning and end of the input date. (Ugh.) . COMPUTE No_Break = 1. ADD FILES /FILE=* /BY No_Break /FIRST=Start_It /LAST =End_it. * ................................................... . * Code generator . * At start, RECODE header . DO IF Start_It EQ 1. . WRITE OUTFILE=Gen_Code /'STRING CorrectFips (A2).' . . WRITE OUTFILE=Gen_Code /'RECODE TO_ST_ABBR' . END IF. * RECODE specifications for each input record . STRING #GenLine (A80). COMPUTE #GenLine = CONCAT( ' ("' ,State_Abbr2 ,'" = "' ,State_FIPS2 ,'")' ). WRITE OUTFILE=Gen_Code /#GenLine. * At end, end of RECODE statement . DO IF End_It EQ 1. . WRITE OUTFILE=Gen_Code /' INTO CorrectFips.'. END IF. * EXECUTE, to make the generator run . EXECUTE. * ................................................... . * The following is the generated code . DATA LIST FILE=Gen_Code FIXED /Gen_Line (A80). Data List will read 1 records from C:\Documents and Settings \...NERATED CODE.GEN Variable Rec Start End Format Gen_Line 1 1 80 A80 LIST. |-----------------------------|---------------------------| |Output Created |23-NOV-2006 00:22:36 | |-----------------------------|---------------------------| Gen_Line STRING CorrectFips (A2). RECODE TO_ST_ABBR ("AL" = "01") ("AK" = "02") ("AZ" = "04") ("AR" = "05") ("CA" = "06") ("CO" = "08") INTO CorrectFips. Number of cases read: 9 Number of cases listed: 9 * ................................................... . * Include the generated code as a lookup table . * (THIS IS NOT TESTED) . GET FILE='E:\IRS Migration Data\' + 'County Migration Files\Co_9304_Master_spare.sav'. DATASET NAME Master_IRS WINDOW=FRONT. INCLUDE Gen_Code. |
Free forum by Nabble | Edit this page |