2
votes

I would like to create variables with a plugin, wich imports a database table. I am using the following code to do this:

SF_macro_save("_vars", "var1 var2...");
SF_macro_save("_types", "type1 type2...");
SF_macro_save("_formats", "format1 format2...");
SF_macro_save("_obs", "obs1 obs2...");

This creates the variables well, but I don't know how to give labels to variables, or to values.

Which C++ function do I need to use to create labels? Or how can I call Stata functions from C++? (I am using Visual Studio 10 if it counts)

I would like to call this Stata functions from the plugin:

label variable var1 label1

and

label define var1_label 1 "label1" 2 "label2" label values var1 var1_label

Thanks

2
Documentation at stata.com/plugins doesn't mention value or variable label assignment. I'd suggest contacting Stata technical support. A wild guess is that you can't do it this way.Nick Cox
Thanks, I couldn't find this topic there either. But maybe someone know this trick :) Anyway, I will try the support.pega

2 Answers

1
votes

This is possible but it is not easy. Basically, you create a .do file in your code (C# example below) then execute the .do file. Here is an example that runs the .do file then puts the results in a SQL Server database using ODBC. You can do something similar with Stat/Transfer to load the data and variable labels into a database.

            string m_stcmd_valuelabels = Server.MapPath("~/Data/Cmd/Stata/") + m_filename_noex + "_valuelables.do";

            using (StreamWriter m_sw_stcmd_valuelabels = new StreamWriter(m_stcmd_valuelabels, false))
            {

                m_sw_stcmd_valuelabels.WriteLine("clear");
                m_sw_stcmd_valuelabels.WriteLine("set mem 500m");
                m_sw_stcmd_valuelabels.WriteLine("set more off");
                m_sw_stcmd_valuelabels.WriteLine("use  \"" + m_fullpath.Replace(".zip", ".dta") + "\"");
                m_sw_stcmd_valuelabels.WriteLine("valtovar _all, dis");
                m_sw_stcmd_valuelabels.WriteLine("uselabel");
                m_sw_stcmd_valuelabels.WriteLine("ren lname varname");
                m_sw_stcmd_valuelabels.WriteLine("drop trunc");
                m_sw_stcmd_valuelabels.WriteLine("odbc insert, dsn(\"MyData\") table(\"" + m_filename_noex + "_valuelabels\") create " + m_statadsn_conn);
                m_sw_stcmd_valuelabels.WriteLine("exit");
                m_sw_stcmd_valuelabels.WriteLine();
            }

            string str_PathValueLabels = Server.MapPath("~/Data/Stata12/StataMP-64.exe");

            ProcessStartInfo processInfoValueLabels = new ProcessStartInfo("\"" + str_PathValueLabels + "\"");
            processInfoValueLabels.Arguments = " /e do \"" + m_stcmd_valuelabels + "\"";
            processInfoValueLabels.UseShellExecute = false;
            processInfoValueLabels.ErrorDialog = false;

            Process batchProcessValueLabels = new Process();
            batchProcessValueLabels.StartInfo = processInfoValueLabels;
            batchProcessValueLabels.Start();
0
votes

You can't do that from the plugin. You can't create variables, labels. etc.. from your dll, the dataset must be defined before you call the plugin, as you probably already know. You can store data values back into the variables, but there's no adding "columns" if you will. You can store the desired names in the macros, but it will fall on your ".do" file to assign them to the variables in Stata, sorry.