BookmarkSubscribeRSS Feed
mkfho
Calcite | Level 5

Hi all,

I was sent an SPSS dataset which I converted to SAS dataset.  Most of the SPSS variables had value labels; for example, for the Sex variable, it was coded as 1='Male" and 2='Female'.  I was able to use the proc format to define all the value labels in SAS.  My goal (i.e., my question) is to make the value labels become the value in the dataset.  For example, instead of 1 and 2 (and the associated format) for Sex, I want the Sex (or a new variable based on Sex) to be Male and Female.  The motivation of doing this is because some of the variables have a few thousand value labels and it's so much easier to just read what the true label of each value.  Plus, I've to merge other variables from another dataset and that dataset only has the labels (not value).  In other words, in the dataset that was imported from SPSS, Sex is 1='Male' and 2='Male'.  The other dataset has Sex has 'Male' and 'Female'.   Even though I used Sex as an example, I'll be merging the two dataset using variables that have thousands of value labels.

Thanks for your assistance.

8 REPLIES 8
Ksharp
Super User

That would be easy if you already have SEX format.

proc format;
value fmt
 1='Male'
 2='Female';
run;
data x;
 a=1;output;
 a=2;output;
 format a fmt.;
run;
data want;
 set x;
 b=vvalue(a);
run;




Xia Keshan

jakarman
Barite | Level 11

The value statement in SPSS is the format statement of SAS. Both are recoding the internal value

Within SPSS the values are stored in the SPSS dataset and with SAS they are own components. Using the SAS formats in work is easily done. In a permanent way you can activate that by setting the fmtsearch option. SAS(R) 9.3 System Options: Reference, Second Edition. This is not needed with SPSS but you need that with SAS.

Having the formats available you can associate those with the variables  SAS(R) 9.3 Statements: Reference By this the representation and the internal value are kept segregated.

The SAS formats are richer than the SPSS values. As you can have the variables getting well readable in both. With SAS is more easy to change formats for recoding or having mulii-value ones.

I would advice to keep the original values untouched and take your benefits from all functionality with formats.

Defining formats is: Base SAS(R) 9.3 Procedures Guide, Second Edition (proc format)    

---->-- ja karman --<-----
bharathtuppad
Obsidian | Level 7

proc format;

value abc 1="Female"

          2="Male";

run;

proc sql;

create table have as

select name, age, case sex

  when "F" then 1

  else 2

   end as ind

from sashelp.class;

select * from have;

quit;

data want;

set have;

sex=put(ind, abc.);

run;

proc print;

run;

mkfho
Calcite | Level 5

Hi Xia, Jaap and bharathtuppad,

Thank you all for your helpful suggestions and syntax codes.  I really appreciate it.  While the code will work if you have a simple variable like Sex with only 2 values, the variables that I was hoping to convert have about 5,000 value labels.  So, the coding time may be too much for the benefit.  I was hoping there would be a simple and quick solution but it doesn't seem to be the case.  Thank you again for your kind assistance.

jakarman
Barite | Level 11

With your last reply your are saying that the question was:

- how to convert SPSS values statements to SAS proc format value statements.

When it is coded clean and you can read and process the SPSS source code it could be done with a SAS program

---->-- ja karman --<-----
Ksharp
Super User

proc format or Hash Table both can do that fast.

Kurt_Bremser
Super User

5000 values are no problem for a SAS value format; as long as you have data that you can convert to cntlin format for the format procedure, you're fine.

art297
Opal | Level 21

You give up too easily! Since you already have and have applied the formats, why not just export and then import the file? E.g.:

/*Create some format and formatted data for testing*/

proc format;

  value $sex

   'M'='Males'

   'F'='Females'

  ;

  value age

   0-15='Younger'

   other='Older'

  ;

  value height

   0-65.5='Shorter'

   other='Taller'

  ;

  value weight

   0-99.9='Lighter'

   other='Heavier'

  ;

run;

data have;

  set sashelp.class;

  format sex $sex.;

  format age age.;

  format height height.;

  format weight weight.;

run;

/*Export the formatted file*/

filename tempfile temp;

proc export data=have outfile=tempfile

dbms=csv replace;

run;

/*Import the formatted file*/

proc import datafile=tempfile out=want

dbms=csv replace;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 10607 views
  • 4 likes
  • 6 in conversation