foreach - Stata Renaming Variables using first line data -


i used insheet read in csv format data. names of variables should first line of dataset. have lot of variables , many years of data read in using foreach var loop follows:

   input str8 x str8 y str8 z    first second third    3 6 2    4 1 2    8 7 6    end     foreach var of varlist * {      rename variable "`var'" "`=`var'[1]'"    } 

unfortunately, receive syntax error response.

i'm assuming problem must in way specified new name. extrapolating q&a: http://www.stata.com/statalist/archive/2011-09/msg01109.html

the problem not foreach. input code didn't run me seems side-issue.

the main problems seem

  1. rename not include syntax variable unless variable name.

  2. rename not use double quotes.

see rename.

this works , keeps on going.

clear  input str8 x str8 y str8 z "first" "second" "third" "3" "6" "2" "4" "1" "2" "8" "7" "6" end  foreach var of varlist * {    rename `var' `=`var'[1]' } drop in 1  destring, replace  l        +------------------------+      | first   second   third |      |------------------------|   1. |     3        6       2 |   2. |     4        1       2 |   3. |     8        7       6 |      +------------------------+ 

however, insheet out-of-date command.

see help import or http://www.stata.com/manuals14/dimport.pdf see import delimited use , supports variable names on first line of data file, none of should needed, unless have access older version of stata.

(if using old version, idea explicit in forum supporting stata questions.)

edit: @roberto ferrer points out insheet supports variable names on first line.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -