SAS Problem: Data infile leads to 0 observations - sas

I'm having an issue with SAS 9.4. See code below;
data myData;
infile 'D:\folder1\folder2\myData.xlsx';
input var1 var2 var3;
This results in SAS executing this successfully, recognizing the 3 variables but containing 0 observations. Is there something wrong with how the code is written? Has anyone encountered this issue? Thank you in advance.

Since an XLSX file is binary (in particular it is just a ZIP file) your data step is not finding any lines of text to read. Most likely the reason you got 0 observations is that when searching for the second or third space delimited word to read it went past the end of the file. So the data step stopped at the INPUT statement and never reached the end of the first iteration to write an observation.
You will need to either use PROC IMPORT or a LIBNAME statement using the XLSX engine to read an XLSX file. Or use Excel to save the file as a delimited text file, then you could read using a simple data step.

If your data is in an Excel format, you should be able to do PROC IMPORT to read it in.
PROC IMPORT DATAFILE="D:\folder1\folder2\myData.xlsx" DBMS=XLSX OUT=myData;
RUN;

Related

How to copy data afrer "cards"/"datalines" in SAS

I have to perform statistical analysis on a file with hundreds of observations and 7 variables(columns)on SAS. I know that it is necessary to insert all the observations after "cards" or "datalines". But I can't write them all obviously. How can I do? Moreover, the given data file already is .sas7bdat.
Then, since (in my case) the multiple correspondence analysis requires only six of the seven variables, does this affect what I have to write in INPUT or/and in CARDS?
You only use CARDS when you're trying to manually write a data set. If you already have a SAS data set (sas7bdat) you can usually use that directly (there are some exceptions but likely don't apply here).
First create a libname to the folder where the file is:
libname myFiles 'path to fodler with sas file';
Then load it into your work library - this is a temporary space that is cleaned up when you're done so no files here are saved permanently.
This copies it over to that library - which is often faster.
data myFileName;
set myFiles.myFileName;
run;
You can just work with the file from that library by referencing it as myFiles.myFileName in your code.
proc means data=myFiles.myFileName;
run;
This should get you started, but you should take the SAS free e-course to understand the basics, it will save you time overall.
Just tell SAS to use the dataset. INPUT statement (and CARDS/DATALINES or INFILE statement) are for reading from text files.
proc corresp data='/my directory/mydataset.sas7bdat' .... ;
...
run;
You could also make a libref that points to the directory and use two level name to reference the dataset.
libname myfiles '/my directory/';
proc corresp data=myfiles.mydataset .... ;
...
run;

How to read data from data file using infile statement?

I am trying to read numeric data from the file. But I am to read it properly the output that I am getting from my SAS program is attached. I recently started learning SAS programming.
I am using SAS University Edition on the windows machine. I already tried by reading data in character as well as numeric formate.
data ds;
infile '/folders/myshortcuts/my_folder/exrate.sas7bdat';
input s ;
run;
I am expecting the same table to be as an output result.
Data File
Output
There are 3 things you need to do:
Make sure that the folder you put the dataset in is accessible to the SAS University Edition VM. Did you follow the setup guide in full? Usually that directs you to set up a folder that becomes available within the VM as /folders/myfolders
Assign a library pointing to the folder using a libname statement.
Use a set statement to access the dataset, not an infile statement. The latter is for reading raw data like csv files.
The sas code should look like this:
libname mylib "/folders/myfolders"; /*Change this to point to your folder path if you're sure you've got the right one*/
data ds;
set mylib.exrate(keep = s);
run;

DDE SAS using VBA Commands

I am trying to write a code using dde in sas which would generalise my cells in excel that is previously formatted to number with 2 decimal places. I don't get any error message in the log but my excel sheet is overwritten with the code that is in the put statement that is RANGE("A1:A3").NumberFormat="General" instead of applying the format to it. I am not sure where I am going wrong. Could someone please help?
Here is my sample code,
NOTE: The Excel engine is opened before I run this code
filename ddeopen dde "EXCEL|System";
options noxwait noxsync;
data _null_;
x=sleep(3);
run;
/* Opens the desired file in Excel*/
DATA _null_;
file ddeopen;
PUT "[OPEN(%bquote("C:\Documents and Settings\S\Desktop\test1.xls"))]";
x=sleep(3);
run;
/* Format the cells to general in the opened Excel sheet*/
filename ddeopen dde "EXCEL|Sheet1!r1c1:r3c1";
data _null_;
file ddeopen;
x=sleep(3);
PUT "[%bquote(RANGE("A1:A3").NumberFormat="General")]";
RUN;
Many Thanks
Simi
I think you need to select your range first using the 'R1:C1' style like so:
put ‘[select("r1c2:r100c2")]’;
Then you can format that selection with the following command:
put '[Format.Number("General")]';
This should then uses the 'General' formatting style for the cells before you export your data.

how to read only specific coulmns from external tab delimited files using proc import into sas dataset

is it possible to read only specific variabiles from text files into sas as sas dataset using proc import? I have very big data in my text file which contains like 1000 observations and more than 42,000 variables. I tried reading this file into sas using proc import but i failed in doing so i thought may be because of size issues. Now i decided to read only specific variables (columns) really i do not need all of them from this big text file so that i can reduce size of file to read into sas system. is there any ideas to read like this by using data step? Suggestion or help would be appreciated
Thanking you very much,
If you're talking about a delimited text file, you can read in specific variables, contingent on them being consecutive from the first variable. You can't use PROC IMPORT to do this, however; you'd have to write the datastep yourself, though you could conceivably have PROC IMPORT help you write it.
For example, if you have 10 variables and only want the first 3, then you can read it in like this:
data want;
infile "mydata.txt" dlm=',' lrecl=255 missover dsd;
input x $ y $ z $;
run;
However, you must read in all variables from the first variable to the last variable you are interested in, even if you aren't interested in all of the intermediate variables. You can't "skip" them with a delimited text file.
If you have a fixed width text file, you can read in whatever columns you want (but you can't use PROC IMPORT to read in a fixed width text file).

Import data from European Social Survey

I need to import data from European Social Survey databank to SAS.
I'm not very good at using SAS so I just naively tried importing the text file one gets but it stores it all in one variable.
Can someone maybe help me with what to do? Since there doesn't seem to be a guide on their webpage I reckon it has to be pretty easy.
It's free to register (and takes 5 secs) and I need all possible data for Denmark.
Edit: When downloading what they call a SAS file, what i get is a huge proc format and the same text file as one gets by choosing text.
The data in the text file isn't comma separated and the first row does not contain variable names.
Download it in SAS format. Save the text file in a location you can remember, and open the SAS file. It's not just one big proc format; it's a big proc format followed by a datastep with input code. It was probably created by SPSS (it fits the pattern of an SPSS saved .sas file anyhow). Look for:
DATA OUT.ESS1_4e01_0_F1;
Or something like that (that's what it is when I downloaded it). It's probably about 3/4 of the way down the page. You just need to change the code:
INFILE 'ESS1_4e01_0_F1.txt';
or similar, to be the directory you placed the text file in. Create a LIBNAME for OUT that goes to wherever you want to permanently save this, and do that at the start of the .sas file, replacing the top 3 lines like so.
Originally:
LIBNAME LIBRARY '';
LIBNAME OUT '';
PROC FORMAT LIBRARY=LIBRARY ;
Change these to:
libname out "c:\mystuff\"; *but probably not c:\mystuff :);
options fmtsearch=(out);
proc format lib=out;
Then run the entire thing.
This is the best solution if you want the formatted values (value labels) and variable labels. If you don't care about that, then it might be easier to deal with the CSV like Bob shows.
But the website says yu can download SAS format, why don't you?
You need a delimiter if all goes into one column.
data temp;
length ...;
infile 'file.csv' dlm=',';
input ...;
run;
As Dirk says, the web site says you can download a SAS dataset directly. However, if there's some reason you don't want to do that, choose a comma separated file (CSV) and use PROC IMPORT. Here is an example:
proc import out=some_data
datafile='c:\path\somedata.csv'
dbms=csv replace;
getnames=yes;
run;
Of course, this assumes the first row contains column names.