Topic
  • 4 replies
  • Latest Post - ‏2013-05-14T18:48:43Z by Cdaggitt
Cdaggitt
Cdaggitt
3 Posts

Pinned topic Need to create X amount of variables

‏2013-05-09T20:14:40Z |

Alright I apologize ahead of time for my lack of expertise here but if anyone can help me with this I need a very BASIC explanation, I have barely ever used this syntax before.

I have a table that ends up looking like this:

A 3
B 0
C 2
D

1

I need to create as many variables as the maximum value in the second column (which here is 3, but this can change), and I'm computing number with one of the  random number generator, and I want them to populate like this:

A 3 x x x
B 0 . . .
C 2 x x .
D 1 x . .

So, the cases will compute values for as many columns as the number in column 2. In pseudo code I was thinking of doing something like this (heads up-I have no idea what of this actually applies to spss syntax):

A=MAX(column2)

While A >0

(If column2 >0 (Compute Var01=gamma(1000,50))

A - 1

column2 - 1)

I have no idea if this logic even works, but if it does I already foresee the need to have the Compute Var01 part to change each time the loop goes through or it's just going to replace the variable. ANY help would be so appreciated.

 

 

 

 

  • JonPeck
    JonPeck
    269 Posts

    Re: Need to create X amount of variables

    ‏2013-05-10T13:13:15Z  

    Here is an example of code to do this.  It presumes that you know the maximum number of fields to create, but you can set this to a largish number and later delete the variables that are all sysmis.  That value could be discovered and incorporated into this syntax using Python programmability, but I'm trying to keep this as simple as possible.

    data list fixed/a(A1) b(F5.0).
    begin data
    A3
    B0
    C2
    D1
    end data.
    dataset name data.
    list.
    vector V(3).
    loop #i = 1 to b.
    compute V(#i) = rv.uniform(0,1).
    end loop.

  • Cdaggitt
    Cdaggitt
    3 Posts

    Re: Need to create X amount of variables

    ‏2013-05-13T13:43:00Z  
    • JonPeck
    • ‏2013-05-10T13:13:15Z

    Here is an example of code to do this.  It presumes that you know the maximum number of fields to create, but you can set this to a largish number and later delete the variables that are all sysmis.  That value could be discovered and incorporated into this syntax using Python programmability, but I'm trying to keep this as simple as possible.

    data list fixed/a(A1) b(F5.0).
    begin data
    A3
    B0
    C2
    D1
    end data.
    dataset name data.
    list.
    vector V(3).
    loop #i = 1 to b.
    compute V(#i) = rv.uniform(0,1).
    end loop.

    Thank you! I will definitely use that for now. For future reference- is there a way for me to do this with X amount of variables instead of a set amount? I feel like I'll need to use a macro or something?

  • JonPeck
    JonPeck
    269 Posts

    Re: Need to create X amount of variables

    ‏2013-05-13T17:49:40Z  
    • Cdaggitt
    • ‏2013-05-13T13:43:00Z

    Thank you! I will definitely use that for now. For future reference- is there a way for me to do this with X amount of variables instead of a set amount? I feel like I'll need to use a macro or something?

    Do you mean that you want to scan the data first and find the actual maximum of b and then use that in the vector and loop statements?

    If so, you would need Python programmability to parameterize it this way.  If not, please clarify what you want to generalize.

  • Cdaggitt
    Cdaggitt
    3 Posts

    Re: Need to create X amount of variables

    ‏2013-05-14T18:48:43Z  
    • JonPeck
    • ‏2013-05-13T17:49:40Z

    Do you mean that you want to scan the data first and find the actual maximum of b and then use that in the vector and loop statements?

    If so, you would need Python programmability to parameterize it this way.  If not, please clarify what you want to generalize.

    I do and I actually figured it all out yesterday- definitely need Python. Thank you!