PROBLEM: I have some data in SAS that uses formats that
I have written, and I want to move the data to
Excel (or Access, or dBase, or any other software
for which SAS can export data), but I want the
values to be the formatted values, not the original
data values.
EXAMPLE: PROC PRINT (without formatting) shows the following
values:
SEX SCORE1 SCORE2
1 42 51
2 65 23
1 43 27
but when I use the variable SEX, I always ask SAS to
employ the user-written format (sexfmt.) where 1='female'
and 2='male', as follows:
SEX SCORE1 SCORE2
female 42 51
male 65 23
female 43 27
SOLUTION: SAS allows the creation of a new variable in which the
formats become the actual data. This is done with an
assignment statement in which the PUT function is used:
NEWVAR=PUT(OLDVAR,FORMAT.);
All values that match the specifications in the format
(i.e., all values that are either 1 ["female"] or 2
["male"]) will have the words stored instead of the
original numbers; all other values -- missing data coded
as 9, for example, or mis-coded values -- will remain the
same, as there is no substitution specified for them in
the format.
*** SAMPLE PROGRAM TO ILLUSTRATE MAKING A NEW VARIABLE THAT
STORES FORMATTED VALUES (RATHER THAN ORIGINAL VALUES);
/** the format is created and stored (in the temporary library) **/
proc format;
value sexfmt 1='female'
2='male';
/** the data are read in as numeric values (PROC PRINT with a
FORMAT statement is used to illustrate the difference between
this and the final step) **/
data temp; input sex score1 score2;
cards;
1 42 51
2 65 23
1 43 27
;
proc print;
format sex sexfmt.;
run;
/** the new variable (GENDER) is created to store the formatted values
as actual data, and PROC PRINT (with no FORMAT statement) is used
to illustrate this new version of the data; notice that the
variable SEX holds the original numeric values, while GENDER
is a character variable with the formatted values (the words
FEMALE and MALE) as actual data; when moved to other software
such as dBase, Access, Excel and so forth, the data values for
GENDER will be 'female' and 'male' (not 1 and 2) */
data temp2; set temp;
gender=put(sex,sexfmt.);
proc print data=temp2;
run;
/**********************************************************************/
*** SAMPLE PROGRAM TO ILLUSTRATE POSSIBLE SHORTCUTS WHEN
MANY VARIABLES ARE INVOLVED;
/*** If many variables have formats assigned to them, it may be
possible to use some handy SAS shortcuts to save programming
time and space. This technique uses the ARRAY language,
and will work if a number of variables use the same
format. (Different variables using different formats will
still have to be recoded separately, as shown above). ***/
EXAMPLE: PROC PRINT (without formatting) shows the following
values:
ID ITEM1 ITEM2 ITEM3 ITEM4 ITEM5
1 1 2 1 1 1
2 2 2 2 9 1
3 2 9 1 1 2
the ITEMn variables are questions on a survey which
ask for a 'yes' or 'no' answer, and they are coded
originally as 1s and 2s. A format called yesno. is
used to allow SAS to display the words instead of the
numbers, if requested. PROC PRINT with a FORMAT
statement makes the output look like:
ID ITEM1 ITEM2 ITEM3 ITEM4 ITEM5
1 yes no yes yes yes
2 no no no 9 yes
3 no 9 yes yes no
Notice that the 9s, which represent missing values, will
stay the same since the format in this example (see below)
doesn't assign any new value to numbers other than 1s and 2s.
/** the format is created and stored (in the temporary library) **/
proc format;
value yesno 1='yes'
2='no';
/** the data are read in as numeric values (PROC PRINT with a
FORMAT statement is used to illustrate the difference between
this and the final step) **/
data temp; input id item1-item5;
cards;
1 1 2 1 1 1
2 2 2 2 9 1
3 2 9 1 1 2
;
proc print;
format item1-item5 yesno.;
run;
/** the new variables (NEWITEMn) are created to store the formatted values
as actual data, and PROC PRINT (with no FORMAT statement) is used
to illustrate this new version of the data; notice that the
variables ITEMn hold the original numeric values, while NEWITEMn
are character variables with the formatted values (the words
YES and NO) as actual data; when moved to other software
such as dBase, Access, Excel and so forth, the data values for
NEWITEMn will be 'yes' and 'no' (not 1 and 2) */
data temp2; set temp; drop i;
array oldies{5} item1-item5;
array newbies{5} $ newitem1-newitem5;
do i=1 to 5;
newbies{i}=put(oldies{i},yesno.);
end;
proc print data=temp2;
run;