SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

 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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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. 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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

 

Emma_at_SAS
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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. 

Emma_at_SAS
Lapis Lazuli | Level 10

Thank you @Tom for your comprehensive explanation!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 4 replies
  • 1336 views
  • 2 likes
  • 2 in conversation