Read Stata version 13 file in SAS 9.4 - sas

I'm trying to import a Stata version 13 file in my SAS 9.4 - but keep getting the error message
For Stata, Release flags of 103 to 115 are supported. You had -> 60
Requested Input File Is Invalid
ERROR: Import unsuccessful. See SAS Log for details.
Folliwing code does not work:
proc import out = uw14 datafile = "C:\User\KE14_hhld.dta"; run;
Neither does this: proc import out = uw14 datafile = "C:\User\KE14_hhld.dta"
dbms = stata replace; run;
I've found out that SAS 9.4 does not read Stata version 13 files. SAS 9.4 only reads Stata 12 files (or earlier).
One solution could be to save the Stata 13 file as an older version - but I only have Stata 12 on my computer and it's unable to open the version 13 file.
Another solution could be to save it as a CSV file in R - this done by following code:
proc import out = uw14 datafile = "C:\User\KE14_hhld.csv"; run;
But I've tried this and the transformation sets an unmanageable amount of my variables as characteristics instead of numeric and kills all formats.
Any other ideas?
(I'm not able to download other versions than SAS9.4 and Stata12)
Thanks

You can try to use R as a kind of third party. You can transform your dta into R data.frame and then write out the data.frame into .sas7bdat.
You can do all of this using the Haven package if I recall correctly. 2 or 3 lines of code should be sufficient:
Haven documentation

You can use stat/transfer for most type of data conversions: https://stattransfer.com

Related

can I overwrite the infile in the SAS code generated by import data wizard

I am using sas enterprise guide 8.3 to import a csv file into SAS.
I try to use import data wizard to do that; then copy the log of it to reuse it later(e.g. next week). the log looks like the following:
/*-------------------------------------------------
code generated by a SAS task
generated on Thursday, August 5,2021 at 9:59:13 PM
By task: import data wizard
Source File:
d:\test\test.csv
Server: Local File System
Output data: Work.temptable
Server: [servername]
-------------------------------------------------------------------*/
Data: Work.temptable;
Length:
col1
col2
Format:
col1
col2
Informat:
col
col2
Infile 'H:\Saswork\TD13012_[Server_name]\#LN_00032'
Input
col1
col2;
run;
My scenario is: I want to save the code above and rerun the code each week because there will be a new csv file each week, I need to import the new csv file each week.
in the comment part, the source file is correct; but in the code part, it seems that infile points to a temp file 'H:\Saswork\TD13012_[Server_name]#LN_00032'. I wonder if this temp file always exists because I need to run the code each week. I try to replace the infile value with the correct location in the comment part(local folder, d:\test\test.csv ), there is an error message.
so how can I handle this infile? thanks!
If the file is on the server where SAS is running then just run the data step to read it. If the file is on the server where EG is running then you will first need to add a step in your process to upload the file to the SAS server before your SAS code can see the file. EG should have "copy files" task you can use
Note that the Import wizard might be modifying the file in the upload process, so make sure your new code works with the original file.
PS You can write a much better data step than what Enterprise Guide generates. For example most variables do not need to have either an INFORMAT or a FORMAT attached to them. SAS already knows how to read and write both character strings and numbers without any need to give it special instructions.

Generating new Variable in SAS results in ERROR 180-322

I am very new to SAS, which is why this question has probably a quite easy answer. I use the SAS university edition.
I have dataset containing socio-structural data in 31 variables and 1000000 observations. The data is stored in a Stata .dta file, which is why I used the following code to import in into SAS:
LIBNAME IN '/folders/myfolders/fake_data';
proc import out= fake_2017 datafile = "/folders/myfolders/fake_data/mz_2017.dta" replace;
run;
Now, I want to create new variables. At first, a year variable that takes the value 2017 for all observations. After, I have several other variables that I want to generate from the 31 existing variables. However, running my code I get the same error message for all my steps:
year = 2017;
run;
ERROR 180-322: Statement is not valid or it is used out of proper order.
I found many things online but nothing that'd help me. What am I doing wrong/forgetting? For me, the code looks like in all the SAS tutorial videos that I have already watched.
You cannot have an assignment statement outside of a data step. You used a PROC IMPORT step to create a dataset named fake_2017. So now you need to run a data step to make a new dataset where you can create your new variable. Let's call the new dataset fixed_2017.
data fixed_2017;
set fake_2017;
year=2017;
run;

Proc Import as Character from Excel Linux SAS

I have a macro that I use to import Excel files from a Windows directory to SAS (version 9.3) on a Linux server. In general the macro has worked fine, but now I'm trying to import an Excel file with a column that contains mostly numeric data with some character records thrown in.
The variable looks something like this:
Var2
1111111
2222222
3333333
4444444
Multiple
5555555
H6666-01
The variable is getting read in as numeric so I'm losing the data in the fifth and seventh records. I've tried a few of the suggestions listed in this answer, but nothing seems to change the variable type.
Here's a portion of the macro I have:
proc import replace
out=&d_set
dbms=excelcs
file="\\path\to\file\&xlsx_nm";
sheet="&sheet_nm";
server="Server";
port=0000;
serveruser="&sysget_USER";
serverpassword="&pw";
range="&rng";
DBDSOPTS = "DBTYPE=(Var2='CHAR(8)')";
run;
I just added the statement DBDSOPTS = "DBTYPE=(Var2='CHAR(8)')"; based on the suggestion on the link above, but the output in the log did not change.
I have also tried padding the original Excel file with a "dummy" record (which I'd like to avoid) with character data in the column that I'm having issues with, but this also did not work.
I'd like to solve this in the import procedure but I'm open to other suggestions.

SAS proc import error: "Didn't see end for |varnames| element. Got -> ||"

I am getting the following error when I try to import a .dta file:
Didn't see end for ¦varnames¦ element. Got -> ¦¦
Requested Input File Is Invalid
ERROR: Import unsuccessful. See SAS Log for details.
The code I am using to import this file is simple, so it should not be a syntax issue. It is of the form below if interested:
proc import datafile = "$path.\data.dta"
dbms=stata
out = data
run;
Any idea what this might be due to? The dataset I am trying to import consists of 4 character variables and ~10,000 obs.
SAS supports version 12 and earlier of Stata through PROC IMPORT currently. You may want to try saving it as a version 12 file and see if that fixes the error. Per #Nick-Cox in comments, see saveold command in Stata to do that.
See PROC IMPORT documentation for more details.

Proper Formatting and Procedures for Basic Data Entry and Manipulation in SAS [duplicate]

I have a data file that I am trying to import into SAS that looks something like the below:
WCM2B W C M 2 B M.B 2 18.4 12.3 g
WCM2B W C M 2 B M.B 2 19.2 12.3 g
WCM2B W C M 2 B S.P 2 19.5 DQ ('')
WCM2B W C M 2 B Z.G 2 17.7 10.7 g
WCM2B W C M 2 B Z.G 2 18.4 10.7 g
WCM2B W C M 2 B Z.G 2 17.6 10.8 g
WCM2B W C M 2 B Z.G 2 20.1 12.1 g
There are headings for each of these columns, some of which list categorical variables some of which do not.
My questions:
1) What is the proper code for ensuring a text file like this, delimited by spaces as shown above and with ~36 rows and 11 columns of data is properly formatted in SAS? How can I then perform operations on this data so that it comes up in the output window? Even the most basic procedure to do on some chosen infile would do. Ideally, if someone is feeling very generous I am trying to get an understanding of how to do regression analysis including analyzing residuals and standard statistics.
2) Do I need to change categorical variables into binary for it to properly analyze the data?
3) Are there any other issues with this data I'm missing that might make prevent it from working?
Thank you very much for your time.
Dealing ONLY with how to read your external file!
Assuming you have a file exactly as described (containing a header row in the first records and fields separated by spaces), you can use PROC IMPORT to read it into a SAS data set:
proc import out=want
datafile='c:\temp\tempdata.txt'
dbms=dlm;
getnames=yes;
delimiter = ' ';
run;
For delimited files like this, SAS uses a tool called the External File Interface to inspect the file and generate regular data-step code to read it. If you look in the SAS log, you will see the actual code that was generated (an infile statement, a set of data definition statements and an input statement). You can use that code as an example to refine the input as needed.
Note that SAS only has two data types (character and numeric). Classifications such as "categorical" and "binary" are matters of usage and not part of a formal data definition. However, certain other SAS tools (such as Enterprise Miner) do allow you to add attributes like this.
To get a simple listing of the data set contents written to the Output window, you can just run simple PROC PRINT:
proc print data=want;
title 'This is my data';
run;
Questions about how to do things like a linear regression on a data set like this are probably beyond the purpose of StackOverflow. There is a wealth of information and examples in the documentation. In your case, start by reading the SAS Concepts book then read about PROC REG in the SAS/STAT Procedures Guide. Here is a link to the main SAS documentation.
I don't have SAS available to test this code. Let me know how it goes.
proc format;
invalue v10fmt "DQ" = .
other = _same_;
run;
data dsname;
informat v10 v10fmt.;
length v1 $5. v2 v3 v4 v6 $1. v7 $3. v11 $1.;
infile "//file/location/and/name" firstobs = 2 delimiter = "09"x;
input v1-v11;
run;
If you have a tab delimited data file, you could consider using PROC IMPORT at least initially.
proc import file="//wherever/myfile.txt" out=mydataset dbms=tab replace;
run;
That will generate a dataset. It will also, usefully, put the input code into the log. You can copy it from the log into your program editor and then make modifications if the import procedure makes poor decisions (for example, it might decide the column with "DQ" should be a character variable). You can adjust that to numeric, and rerun the pasted code.
Now you can do whatever you want to that dataset. You can do things like
proc freq data=mydataset;
run;
The rest of your questions are really general research questions that can't be easily answered without both knowing your analysis and having a lot of time to write answers :) I would recommend doing some reading online on data analysis; these aren't really issues specific to SAS, but are general research guidelines, and there are lots of papers out there on the topics.