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;
@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
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!
@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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.