Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referring to a particular element within a numeric variable

    Hi all,

    I have an identifier variable in my dataset which is coded as "XYZ" where X,Y, and Z can take on the numbers 1 to 5. I would like to look at the observations where Y=2 for the identifier variable. Is there any way to refer to the second or any other element of a variable?

    Thank you.

  • #2
    Perhaps these two examples will start you in a useful direction.
    Code:
    . generate ident_s = strofreal(ident,"%3.0f")
    
    . generate wanted = substr(ident_s,2,1) == "2"
    
    . generate x = floor(ident/100)
    
    . generate y = floor(ident/10)-10*x
    
    . generate z = ident - 10*y - 100*x
    
    . generate wanted2 = y==2
    
    . list, clean
    
           ident   ident_s   wanted   x   y   z   wanted2  
      1.     123       123        1   1   2   3         1  
      2.     234       234        0   2   3   4         0  
      3.     345       345        0   3   4   5         0  
      4.     524       524        1   5   2   4         1

    Comment


    • #3
      Originally posted by William Lisowski View Post
      Perhaps these two examples will start you in a useful direction.
      Code:
      . generate ident_s = strofreal(ident,"%3.0f")
      
      . generate wanted = substr(ident_s,2,1) == "2"
      
      . generate x = floor(ident/100)
      
      . generate y = floor(ident/10)-10*x
      
      . generate z = ident - 10*y - 100*x
      
      . generate wanted2 = y==2
      
      . list, clean
      
      ident ident_s wanted x y z wanted2
      1. 123 123 1 1 2 3 1
      2. 234 234 0 2 3 4 0
      3. 345 345 0 3 4 5 0
      4. 524 524 1 5 2 4 1
      Thank you! Converting the variable into a string and extracting the sub-string looks like a good way to approach this.

      Comment

      Working...
      X