pega
pega

Reputation: 23

How can I give labels to variables in Stata from macro

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

Upvotes: 2

Views: 520

Answers (2)

Paul
Paul

Reputation: 94

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.

Upvotes: 0

smoore4
smoore4

Reputation: 4866

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();

Upvotes: 1

Related Questions