I want to use PROC IMPORT to read an SPSS dataset into SAS and bring with it the value and variable labels (It looks like when I export from SPSS to SAS, I can only bring the value labels and not the variable labels). May you please edit my code below to include the value and variable labels?
PROC IMPORT
DATAFILE= "C:\SPSSdata.sav"
OUT=SASdata
DBMS=sav REPLACE;
RUN;
Thanks
In SPSS you have to repeat the code/decode logic for every individual variable. So if you have 10 variables that are using 1 to YES and 0 for NO you have to repeat that 10 times.
In SAS you can create an independent thing called a FORMAT to store the code/decode pairs. So you can define one format named for example YESNO that will map 1 to 'YES' and 0 to 'NO'. You can then use that format with any variable that has the right set of coded values. Note that PROC IMPORT does not attempt to eliminate duplicate format definitions that it creates. So if you have the aforementioned 10 YES/NO variables it will create 10 independent formats.
Formats live in a format CATALOG (not a dataset). Formats are separate from the dataset. So if you give someone a dataset that is using user defined formats you have to also give them the format catalog so they can find the formats.
You can use PROC FORMAT to create a dataset that describes the formats.
proc format noprint lib=work.formats cntlout=work.formats;
run;
You can reverse the process by using the CNTLIN= option.
To tell SAS where to look for formats you can use the FMTSEARCH option. By default the FMTSEARCH option will include the WORK.FORMATS catalog in the list of places it looks for formats. So if you use the PROC IMPORT code I showed before you can then immediately use the created dataset and the formats will be found. Remember that files in the WORK library are removed when you SAS session ends. So if you want to not have to re-import the .SAV file next time then modify the PROC IMPORT code to write the dataset and format catalog to a permanent library.
To make it easier to modify the FMTSEARCH option setting you can use the APPEND or INSERT option.
You need to tell PROC IMPORT where to place the formats that it creates from the "value" labels.
So add another statement to the PROC IMPORT step.
PROC IMPORT DBMS=sav
DATAFILE= "C:\SPSSdata.sav"
OUT=SASdata REPLACE
;
fmtlib=work.formats;
RUN;
See example 5 in the documentation. https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acpcref/n1h6b01uh0dm2yn1fpq85gjgwcfs.htm
Thank you, @Tom.
1- Then, with this addition, PROC IMPORT saves the value labels in a dataset called FORMATS in the WORK library?
2 - And, I cannot import the variable labels, right?
Thanks
In SPSS you have to repeat the code/decode logic for every individual variable. So if you have 10 variables that are using 1 to YES and 0 for NO you have to repeat that 10 times.
In SAS you can create an independent thing called a FORMAT to store the code/decode pairs. So you can define one format named for example YESNO that will map 1 to 'YES' and 0 to 'NO'. You can then use that format with any variable that has the right set of coded values. Note that PROC IMPORT does not attempt to eliminate duplicate format definitions that it creates. So if you have the aforementioned 10 YES/NO variables it will create 10 independent formats.
Formats live in a format CATALOG (not a dataset). Formats are separate from the dataset. So if you give someone a dataset that is using user defined formats you have to also give them the format catalog so they can find the formats.
You can use PROC FORMAT to create a dataset that describes the formats.
proc format noprint lib=work.formats cntlout=work.formats;
run;
You can reverse the process by using the CNTLIN= option.
To tell SAS where to look for formats you can use the FMTSEARCH option. By default the FMTSEARCH option will include the WORK.FORMATS catalog in the list of places it looks for formats. So if you use the PROC IMPORT code I showed before you can then immediately use the created dataset and the formats will be found. Remember that files in the WORK library are removed when you SAS session ends. So if you want to not have to re-import the .SAV file next time then modify the PROC IMPORT code to write the dataset and format catalog to a permanent library.
To make it easier to modify the FMTSEARCH option setting you can use the APPEND or INSERT option.
Thank you @Tom for your comprehensive explanation!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: