The SAS Output Delivery System and reporting techniques

Importing SPSS File (.sav) into SAS System --Urgent

Reply
N/A
Posts: 0

Importing SPSS File (.sav) into SAS System --Urgent

Hi All,

I being trying to import a SPSS file, with extension ".sav", into the SAS system. So far I can use proc import to read it in, but the data set's format is incorrect. Unfortunately I don't have SPSS to convert ".sav" to ".port", so this option is not available to me. Please kindly let me know what else I can do to read in this file or how to adjust the formatting issue. Your help is much appreciated!!!

Thanks,
Robert
SAS Employee
Posts: 174

Re: Importing SPSS File (.sav) into SAS System --Urgent

This question is perhaps best answered by Technical Support. You can submit it online at http://support.sas.com/ctx/supportform/index.jsp .

-- David Kelley, SAS
Occasional Contributor
Posts: 6

Re: Importing SPSS File (.sav) into SAS System --Urgent

I have a macro in my blog to do so. But please read the instruction carefully.

MACRO TO READ *.sav OR *.dta INTO SAS
%macro foreign(file = , data = );
/************************************************
* THIS MACRO IS MOTIVATED BY FOREIGN PACKAGE IN *
* R LANGUAGE AND DESIGNED TO READ SPSS *.sav OR *
* STATA *.dta DATA FILE. *
*************************************************
* REQUIREMENTS TO RUN THIS MACRO: *
* 1. INSTALL R LANGUAGE (www.r-project.org) *
* 2. INSTALL FOREIGN PACKAGE *
*************************************************
* MACRO INPUTS: *
* 1. file: full path of data file. *
* (example: data = c:\temp\test.sav) *
* 2. data: name of output SAS table *
************************************************/

/* PATH THAT R IS INSTALLED ON PC */
%let rpath = C:\Program Files\R\bin\;

data _null_;
/* CHANGE PATH OF DATA FILE THAT R CAN RECOGNIZE */
in = tranwrd("&file", "\", "\\");
call symput('in', trim(in));
/* ASSIGN THE DIRECTORY OF DATA FILE TO MACRO VARIABLE */
path = substr("&file", 1, index("&file", scan("&file", -1, "\")) - 1);
call symput('path', trim(path));
/* ASSIGN NAME OF R FILE TO MACRO VARIABLE */
r = trim(path)||"foreign.r";
call symput('r', trim(r));
/* ASSIGN PATH OF OUTPUT *.csv FILE TO MACRO VARIABLE */
csv = tranwrd(trim(path)||"&data"||".csv", '\', '\\');
call symput('csv', trim(csv));
run;

data _null_;
/* GET THE EXTENSION OF DATA FILE, .sav OR .dta */
ext = upcase(scan("&file", -1, "."));
/* OUTPUT A FILE WITH R CODE */
file "&r";
/* WRITE R CODE TO OUTPUT R FILE */
put 'cat("\n", "***** Log of R execution *****", "\n\n");';
put "library(foreign);";
if ext = 'SAV' then do;
imp = "&data"||'<-read.spss(file = "'||"&in"||'", '||
"use.value.labels = FALSE, to.data.frame = TRUE);";
end;
if ext = 'DTA' then do;
imp = "&data"||'<-read.dta(file = "'||"&in"||'", '||
"convert.underscore = FALSE);";
end;
put imp;
exp = 'write.table('||"&data"||', file = "'||"&csv"||'", '||
'sep =",", row.names = FALSE, na = " ");';
put exp;
put 'quit("no");';
/* END OF R FILE */
/* ASSIGN SHELL SCRIPT TO RUN R LANGUAGE TO MACRO VARIABLE */
cmd = "'"||'"'||"&rpath.Rcmd.exe"||'" BATCH -- "'||
"&r"||'"'||"'";
call symput('cmd', trim(cmd));
run;

/* RUN R CODE IN BATCH MODE */
options XSYNC NOXWAIT XMIN;
x &cmd;

/* IMPORT *.csv FILE OUTPUT BY R INTO SAS */
proc import datafile = "&path.&data..csv"
out = &data replace;
getnames = YES;
run;

data _null_;
/* INFILE R OUTPUT */
infile "&r..Rout";
input;
/* SHOW R OUTPUT IN SAS LOG WINDOW */
if _n_ > 20 then put _infile_;
/* ASSIGN NAMES OF INTERMEDIATE FILES TO MACRO VARIABLES */
del1 = "'del "||'"'||"&r"||'"'||"'";
call symput('del1', trim(del1));
del2 = "'del "||'"'||"&path.&data..csv"||'"'||"'";
call symput('del2', trim(del2));
del3 = "'del "||'"'||"&r..Rout"||'"'||"'";
call symput('del3', trim(del3));
run;

/* DELETE INTERMEDIATE FILES */
systask command &del1;
systask command &del2;
systask command &del3;

/************************************************
* END OF MACRO *
************************************************/
%mend foreign;
Ask a Question
Discussion stats
  • 2 replies
  • 235 views
  • 0 likes
  • 3 in conversation