Announcement

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

  • creating a variables using two variable

    Dear Statalist Users,

    Based on these two variables, I need to create a new variable named as caseid where I intend to merge HHID and HVIDX in such a manner:

    For example, caseid will be "20010001 01" for the first one and caseid will be "20010001 02" for the second one, and goes like this. Meaning that, if HVIDX is from 1 to 9, I need 0 before the number.

    If HVIDX is 10, for example, I need caseid to appear (e.g.,) "20010001 10". Can you please help me? Thank you very much in advance. I am also attaching a second picture of the caseid variable I want:
    Click image for larger version

Name:	q1.png
Views:	1
Size:	8.9 KB
ID:	1698347
    Click image for larger version

Name:	q2.png
Views:	1
Size:	4.9 KB
ID:	1698348

  • #2
    Please read FAQ Advice #12 on why screenshots are less useful than you hope and why you should use dataex.


    Code:
    clear
    set obs 10
    gen long hhid = 20010000 + _n
    gen HHID = strofreal(hhid, "%8.0f")
    gen HVIDX = _n
    
    * start here 
    gen wanted = HHID + " " + strofreal(HVIDX, "%02.0f")
    
    list

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Please read FAQ Advice #12 on why screenshots are less useful than you hope and why you should use dataex.


      Code:
      clear
      set obs 10
      gen long hhid = 20010000 + _n
      gen HHID = strofreal(hhid, "%8.0f")
      gen HVIDX = _n
      
      * start here
      gen wanted = HHID + " " + strofreal(HVIDX, "%02.0f")
      
      list
      Dear Nick,

      Thank you so much. I will use dataex from now on 😊

      Comment

      Working...
      X