SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  One IF command, multiple tasks?

    Posted Thu March 30, 2023 09:16 AM

    Hi,

    I am attempting to write a SPSS syntax which allows me to match region and state (created variables) based on postcode (input variable).

    I have a list of all postcodes in Australia, together with region and state, similar to the table below. 

    Postcode Region State
    2000 Sydney New South Wales
    3000 Melbourne Victoria
    4000 Brisbane Queensland

    I will on a regular basis receive datafiles that only contain postcode. Based on the postcode I want to create a syntax that autofill name of the region and state. I could merge datafiles but prefer to have everything in a syntax file. Is there an IF statement that allows me to alter more than one variable at a time?

    Something like the one below:

    Alter type POSTCODE (F10). 

    String REGION  STATE (A50). 

    If (POSTCODE=2000) REGION="Sydney", STATE="New South Wales'. 

    If (POSTCODE=3000) REGION="Melbourne', STATE="Victoria". 

    If (POSTCODE=4000) REGION="Brisbane', STATE="Queensland". 

    Etc.

    Any suggestions on how to do this?

    Kind regards,

    Amanda



    ------------------------------
    Amanda
    ------------------------------


  • 2.  RE: One IF command, multiple tasks?

    Posted Thu March 30, 2023 09:56 AM
    Hi, 

    this might be what you are looking for.


    DO IF POSTCODE = 2000.
        COMPUTE REGION = "Sydney".
        COMPUTE STATE="New South Wales".
    ELSE IF POSTCODE = 3000.
        COMPUTE REGION="Melbourne".
        COMPUTE STATE="Victoria".
    ELSE IF... .
        ... .
    END IF.
    EXECUTE.





  • 3.  RE: One IF command, multiple tasks?

    Posted Thu March 30, 2023 10:07 AM
    Also: If you have a priori information about the relative frequency of the individual postal codes, you could put the more frequent ones at the beginning. This way the syntax will run faster, because on average of all cases less ELSE IF branches are executed. Especially in large datasets this might make a noticeable difference. 






  • 4.  RE: One IF command, multiple tasks?

    Posted Thu March 30, 2023 10:14 AM
    If there is a really long list, creating a dataset with the postal codes and associated information and using MATCH FILES with a table lookup would be the way to go rather than coding a lot of syntax for this.

    --





  • 5.  RE: One IF command, multiple tasks?

    Posted Thu March 30, 2023 10:25 AM
    Agreed, absolutely. I just honored the initial rejection of table lookups.