BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
David_SAS
SAS Employee
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
wensui
Calcite | Level 5
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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1283 views
  • 0 likes
  • 3 in conversation