DATA Step, Macro, Functions and more

Export to Stata with uppercase variable names

Reply
Regular Contributor
Posts: 151

Export to Stata with uppercase variable names

I want to export a SAS file to a Stata file. The variable names are in upper case in SAS and I need them to be in upper case in the Stata file. Can this be done? The code below produces a stata file where the variable name is in lower case. (The validvarname option doesn't seem to have any impact).

options validvarname=upcase;
data test;
VAR = 1;
output;
run;
proc export data=test outfile="c:\temp\test.dta" replace;
run;
Super User
Posts: 13,950

Re: Export to Stata with uppercase variable names

Posted in reply to BruceBrad

@BruceBrad wrote:

I want to export a SAS file to a Stata file. The variable names are in upper case in SAS and I need them to be in upper case in the Stata file. Can this be done? The code below produces a stata file where the variable name is in lower case. (The validvarname option doesn't seem to have any impact).

options validvarname=upcase;
data test;
VAR = 1;
output;
run;
proc export data=test outfile="c:\temp\test.dta" replace;
run;

The validvarname option controls how SAS data set names are built and I would not expect it to affect export to STATA.

Note that

options validvarname=upcase;
data test;
var= 1;
output;
run;

creates the test data set with variable VAR (uppercase).

 

I have to assume you forgot to include the DMBS=STATA option for proc export when posting and not during your actual test.

 

You might try providing an upper case label in the SAS data set and use the Label option in proc export. I don't have STATA so can't see that makes a difference or not.

 

Or look for Stata equivalent of Proc Datasets to rename variables if case is actually important in Stata. I wouldn't be surprised if SAS explicitly uses lower case on export to stata for some reason.

 

Another option would be 1) ensure the variable names in SAS are uppercase, 2) export to CSV and 3) then import to Stata

Regular Contributor
Posts: 151

Re: Export to Stata with uppercase variable names

I ended up using Stata to rename the variables. (rename *,upper). In this case it wasn't such a problem since I wanted everything uppercase. However, it is a shame that SAS doesn't write out the variables while keeping the case as defined in SAS. (Stata variable names are case sensitive so this is important).

 

Incidentally:

- the DBMS=Stata is not needed if the file type is .dta

- the label option in proc export replaces the variable name with SAS variable label. However, it also converts it to lower case!

Super User
Posts: 13,950

Re: Export to Stata with uppercase variable names

Posted in reply to BruceBrad

@BruceBrad wrote:

I ended up using Stata to rename the variables. (rename *,upper). In this case it wasn't such a problem since I wanted everything uppercase. However, it is a shame that SAS doesn't write out the variables while keeping the case as defined in SAS. (Stata variable names are case sensitive so this is important).

 

- the label option in proc export replaces the variable name with SAS variable label. However, it also converts it to lower case!


I suggest bringing this to the attention of Tech support and making a suggestion to address this issue of name case as well as putting a thread on the topic here: https://communities.sas.com/t5/SASware-Ballot-Ideas/idb-p/sas_ideas

 


- the DBMS=Stata is not needed if the file type is .dta


Perhaps from dinosaur-like tendencies when it comes to file import, export or reading/writing with a data step I dot the I, cross the t and ensure that the delimiter I specify and dbms are specified even if a default might work. That way when I use or read the code a few years later I know what the file formats involved actually were.

 

If you search this forum you will find posts of the type that "I tried to import a CSV file but it didn't work." After several go-a-rounds it is finally revealed that the file named something.csv is delimited with ; or | or tab or not delimited at all but is fixed column. Another reason I get a bit leery of relying on filenames/extensions to determine type.

Ask a Question
Discussion stats
  • 3 replies
  • 169 views
  • 0 likes
  • 2 in conversation