Announcement

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

  • Understanding renaming based on wildcards

    How I thought I understood wildcards was that both of these series of variables should be renamed to keep only what's after the suffix (in this instance, the full 4-digit year), but obviously this is not the case based on the below example and I'm not sure what I'm misunderstanding. How would I properly rename the HGC variables?

    Code:
    clear
    input HGC08_2008 HGC10_2010 DBY2008_2008 DBY2010_2010
    1 1 1 1
    end
    
    
    rename HGC*_* HGC_*
    rename DBY*_* DBY_*
    
    list 
    HGC_08    HGC_10    DBY_2008    DBY_2010
    Last edited by Taylor Walter; 25 Oct 2024, 15:42.

  • #2
    Wildcards are matched 1:1, meaning the first (and only) asterisk in the new name pattern matches the first (of two) asterisk in the old name pattern.

    There are various possibilities to do what you want; I believe one is

    Code:
    rename HGC*_* HGC_*[2]
    where you use explicit subscripts.

    An alternative should be

    Code:
    rename HGC*_* HGC._*
    where the . (dot) skipps the respective wild card from the old name pattern.

    You could also use # wildcards to match digits, but I am not going there.

    Note that I am away from my computer and cannot test my code.

    Edit: It should now be obvious that your code works for the DBY variables because both wildcards match the same characters.
    Last edited by daniel klein; 25 Oct 2024, 17:14.

    Comment

    Working...
    X