I think this is a rather simple problem but the solution is escaping me. I have a data set of some number of records and variables. The data have to be written into an Excel file with a very specific format and a specific variable order. I need to massage the raw data some and then I want to write the data to a preformatted Excel file that has the variable names in row 1.
Ideally, the foreach loop would execute the putexcel statements, move to the next record in the data set, and write the values from that record into the next line in the Excel file. But what happens with the code below is that the values from the very first record in the Stata data set get written to however many rows are specified in the loop. In this case, there were 4 records in the test data set I am working with. The data were written to Excel in the correct locations but the data from record 1 was repeated across all 4 rows.The loop does not advance across the records in the data set.
How do I tell Stata to write a row of data, proceed to the next record in the data file, and write the date from that record into the next row of the Excel file, repeating this until the end of the Stata data set has been reached and there is a separate row in the Excel file for each record in the data set?
I know I probably could figure out a way using order and export to make it work but I would prefer to manage it this way if possible.
Thanks for any help.
James
Ideally, the foreach loop would execute the putexcel statements, move to the next record in the data set, and write the values from that record into the next line in the Excel file. But what happens with the code below is that the values from the very first record in the Stata data set get written to however many rows are specified in the loop. In this case, there were 4 records in the test data set I am working with. The data were written to Excel in the correct locations but the data from record 1 was repeated across all 4 rows.The loop does not advance across the records in the data set.
How do I tell Stata to write a row of data, proceed to the next record in the data file, and write the date from that record into the next row of the Excel file, repeating this until the end of the Stata data set has been reached and there is a separate row in the Excel file for each record in the data set?
I know I probably could figure out a way using order and export to make it work but I would prefer to manage it this way if possible.
Thanks for any help.
James
Code:
putexcel set "HAS NEXA GPRA $year - Quarter $quarter.xlsx", modify sh("CSATGPRAClientOutcomeMeasuresCS") forvalues i = 2/$rec_total { quietly: putexcel A`i' = has_client_id quietly: putexcel B`i' = grant_type quietly: putexcel C`i' = 0 quietly: putexcel D`i' = contract_id quietly: putexcel E`i' = interview_type quietly: putexcel F`i' = interview_type quietly: putexcel G`i' = (baseline_interview_date) }
Comment