I have a Excel-sheet, which contains variable and variable-labels. I would like to import this file into Stata. How can I do it?
Let's assume that your variable names are on the first row, and labels on the second row.
I would do:
import excel using file.xlsx, firstrow clear
foreach var of varlist _all {
local x = `var'[1]
label var `var' "`x'"
}
drop if [_n]==1
foreach var of varlist _all {
cap destring `var', replace
}
The first bit replaces the label of the variables with the variable label, which should be in the first row of your imported dataset. The second bit drops this row, and the destrings all variables for which this is possible without an error. The reason for this is that all variables will be imported as strings when you have the second row as variable labels.
This is the most typical case I encountered, but of course there may be other scenarios where you have to adopt different approaches.
Related
I'm designing a Mastermind game, which basically compares 2 lists and marks the similarities. When a colour is found at the right place, a flag making the correct position is added and the item found on the reference list is marked off. The reference list is feeding off an array from another function. The problem is at the mark off, as any changes done to the reference list is changing also the original array, which i don't want it to happen
tempCode = mCode #mCode is the array combination randomly generated from another function
for i in range (len(uCode)): #user input array
for j in range (len(tempCode)): #temp array
if uCode[i] == tempCode[j]: # compare individual chars
if i == j: #compare position
flagMark = "*"
tempCode.insert(j+1, "x") #problem starts here
tempCode.remove(tempCode[j])
fCode.append(flagMark)
When the insert is reached both the tempCode and mCode change which it is not intended.
The code is written in a way should the user enter a combination of the same colours, thus checking the chras(the colours are just letters) and the position, and then mark them of with "x"
As it stands, when it gets to
tempCode.insert(j+1, "x")
the arrays will change to
mCode = ["B","R","x","G","Y"]
tempCode = ["B","R","x","G","Y"]
when I would just want
mCode = ["B","R","G","Y"]
tempCode = ["B","R","x","G","Y"]
See also this answer, which is a different presentation of the same problem.
Essentially, when you do tempCode = mCode, you're not making a copy of mCode, you're actually making another reference to it. Anything you do to tempCode thereafter affects the original as well, so at any given time the condition tempCode == mCode will be true (as they're the same object).
You probably want to make a copy of mCode, which could be done in either of the following ways:
tempCode = mCode.copy()
tempCode = mCode[:]
which produces a different list with the same elements, rather than the same list
I am trying to get some variables and numbers out from an Excel table using Matlab.
The variables below named "diffZ_trial1-4" should be calculated by the difference between two columns (between "start" and "finish"). However I get the error:
Undefined operator '-' for input arguments of type"
'cell'.
And I have read somewhere that it could be related to the fact that I get {} output instead of [] and maybe I need to use cell2mat or convert the output somehow. But I must have done that wrongly, as it did not work!
Question: How can I calculate the difference between two columns below?
clear all, close all
[num,txt,raw] = xlsread('test.xlsx');
start = find(strcmp(raw,'HNO'));
finish = find(strcmp(raw,'End Trial: '));
%%% TIMELINE EACH TRIAL
time_trial1 = raw(start(1):finish(1),8);
time_trial2 = raw(start(2):finish(2),8);
time_trial3 = raw(start(3):finish(3),8);
time_trial4 = raw(start(4):finish(4),8);
%%%MOVEMENT EACH TRIAL
diffZ_trial1 = raw(start(1):finish(1),17)-raw(start(1):finish(1),11);
diffZ_trial2 = raw(start(2):finish(2),17)-raw(start(2):finish(2),11);
diffZ_trial3 = raw(start(3):finish(3),17)-raw(start(3):finish(3),11);
diffZ_trial4 = raw(start(4):finish(4),17)-raw(start(4):finish(4),11);
You are right, raw contains data of all types, including text (http://uk.mathworks.com/help/matlab/ref/xlsread.html#outputarg_raw). You should use num, which is a numeric matrix.
Alternatively, if you have an updated version of Matlab, you can try readtable (https://uk.mathworks.com/help/matlab/ref/readtable.html), which I think is more flexible. It creates a table from an excel file, containing both text and numbers.
I´m using Stata and I have a set of variables named cal1, cal2, cal3 and so on until cal21. For every line of my dataset, i could have more or less cal* variables as non-missing (I designed the dataset with a reshape wide). I want to generate a new variable that returns the maximum name of variable cal* available for each line that is non-missing. For example, if line 1 has until cal3 as non- missing , this variable returns cal3; for the line 2 if i have cal1, cal2 and cal6, I want cal6. Is there a way to do this?
This would be much easier to accomplish with data in long format layout, but it is doable with wide data too with a loop:
gen max_cal = "none"
forvalues v=1/21 {
replace max_cal = "cal`v'" if !missing(cal`v')
}
This will update the max_cal variable each time there's a higher one not missing.
I have many variables. For brevity, assume I have two: Gender and Meal. In Stata, I am using tabout, a package that allows one to produce .tex based on Stata results that can be opened as tables in LaTeX.
In order to create a customized output with a little spacing before the variable labels, I want to assign a prefix, \hspace{0.3cm}, to the beginning of all of the values (not labels) of each variable. How can I do this automatically with a loop instead of manually doing this?
Let's say I start out with this:
label def gen 0 "Male" 1 "Female", modify
label value Gender gen
label def me 0 "Lunch" 1 "Dinner", modify
label value Meal me
I want to have a loop that will automatically add the prefix to the individual values of Gender and Meal. The end result would be the same as if I had originally done:
label def gen 0 "\hspace{0.3cm}Male" 1 "\hspace{0.3cm}Female", modify
label value Gender gen
label def me 0 "\hspace{0.3cm}Lunch" 1 "\hspace{0.3cm}Dinner", modify
label value Meal me
Note that code (from http://www.jwe.cc/2012/03/stata-latex-tables-estout/) to do a similar thing for variable labels (and NOT values) is as follows:
foreach v of varlist * {
label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
}
Here is some code that produces the strings you want. I leave to you defining the new value labels and assigning to the variables. Let us know if it's useful.
clear all
set more off
*----- example -----
label def gen 0 "Male" 1 "Female", modify
*label value Gender gen
label def meal 0 "Lunch" 1 "Dinner", modify
*label value Meal me
*----- what you want -----
label dir
local rnames `=r(names)'
foreach labname of local rnames {
quietly label list `labname'
local myname
forvalues i = 0/`r(max)' {
local name : label `labname' `i', strict
local newname \hspace{0.3cm}`name'
local myname `myname' `newname'
}
display "`myname'"
}
You can make it a bit shorter, but it's all very "explicit".
help label and help extended_fcn are a must-read.
(I still insist that a solution within tabout is maybe possible; but I can't be sure.)
Edit
The following is more general, has better form and is a complete example. Extended macro functions are still the basis for the code.
clear all
set more off
*----- example database -----
sysuse voter
*----- what you want -----
foreach var of varlist _all {
local cnewname
quietly labellist `var'
if "`r(lblname)'" != "" {
*disp "`var'"
forvalues i = 1/`r(`r(lblname)'_k)' {
local val : word `i' of `r(values)'
local labval : word `i' of `r(labels)'
local newname `val' "\hspace{0.3cm}`labval'"
local cnewname `cnewname' `newname'
} // forvalues
label define newlbl`var' `cnewname'
label value `var' newlbl`var'
} // if
} // foreach
labellist
I define new value labels and re-associate with corresponding variables. You can try replacing or whatever fits your needs.
Stata doesn't understand TeX or LaTeX, at least not like this.
You could just prefix with space(s), but often Stata would just ignore them any way.
A bizarre trick I've used occasionally is to use char(160) as a pad which looks like a space but won't be trimmed.
length(trim("`=char(160)'"))
is reported as 1, i.e. char(160) is not trimmed. To check that char(160) is invisible on your machine,
di char(160)
But how this works surely depends on your TeX/LaTeX code and how it treats that character.
I have imported a matrix X filled with data, and its according headers for each column into MATLAB. Now the problem is how can I rename each column of X by its according name in the header cell.I would like to do this in a loop.
Would anyone tell me how can I loop a rename programme in this situation?
I suggest creating a structure out of the data, rather than individual variables. Even with a large number of columns, this will not clutter the workspace, nor will it overwrite variables already in the workspace in the case of a name collision. It will keep all the data from the spreadsheet together, and still allowing access to it by column name. To easily create a structure from a cell array of column names and a matrix of data, use cell2struct:
>> colnames = {'odds','evens'};
>> data = [1 2;3 4;5 6];
>> spreadsheet_structure = cell2struct(num2cell(data,1), colnames, 2)
spreadsheet_structure =
odds: [3x1 double]
evens: [3x1 double]
(num2cell(M,1) creates a cell array in which each cell is a column from matrix M)
Loop through the header columns and use eval to create variables with names contained as strings in your matrix "header":
[X,header,~] = xlsread('eaef21.xls',1,'A1:AY541');
for H = 1:size(header,2)
eval([header(1,H), " = X(:,", H, ");"]);
end
Also it is often very useful to replace the eval above with disp until you are satisfied that it is working as you want it to. Using disp will help you understand what is going on as well.