BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

Yeah, i bet you hear this a lot from spss users also using sas. I'm going to assume that when i say spss variable labels and value labels commands you know what i mean in terms of their functions.

After looking at Ron Cody's book, I thought the sas label function and the proc format would give me what i want but they are useless. You run variables singly and/or in combination through proc freq and then put the mht file up for viewing by people who are just one step (the PIs), let alone two or three steps (pre and post docs and project staff) removed from the analyst and the output is not understandable without each person having a data dictionary in front of them. I'm sure you've heard this many times before but is there a solution?

Thanks, Gene Maguin

 

4 REPLIES 4
Patrick
Opal | Level 21

You can in SAS assign formats and labels permanently to variables so that people looking at your data see the variable labels and the formatted values.

ballardw
Super User

Concrete examples please.

 

You will find that if you use the correct options from SPSS to export to SAS one of the files created is Proc Format code.

 

One might suspect that HOW to create the format and associate it with the variable(s) might be an issue. It sounds to me like you did not associate the format with the variables for use by proc freq.

 

Using the SASHELP.CLASS data set, which is usually installed by default you can see the default output for:

Proc print data=sashelp.class;
run;

To use a format with a value there are two basic steps: make the format available to SAS session if it is not one of the SAS supplied formats and then tell the procedure to use the format.

Run this brief code to see the example of using Proc Format in your session to make a format available and the use in Proc Freq.

proc format;
value $sex
'F' = 'Female'
'M' = 'Male'
;
run;

proc print data=sashelp.class;
   format sex $sex.; /*<= this statement associates the format with the variable*/
run;

Notice that nothing is done to the data, just assigning a format.

A format can be made a default for a variable when it is created, in a data step (in effect recreating it but that's another dicussion) or the utility procedure Proc DATASETS that lets you change (some) properties of datasets and the variables in them in place.

 

The 'make a format available' can also be done by placing the formats into a permanent library, an option on the proc format statement, and then making sure that format location is in the SAS system format search path done with a system setting or option statement in your code.

 

One of the extremely flexible advantages of the Format as opposed to value labels (which behave as a permanently associated custom format) is that they can be overridden for a specific display or create groups for analysis without changing the data set at all. The above example does that. The default format permanently associated with the variable Sex in the SASHELP.CLASS data set is $1., which shows one letter. The Proc Freq overrides that format in the second example for display, but we have not changed the data.

Groups created with format will be honored by most of the analysis, reporting or graphing procedures.

Consider

proc format;
value classage
10 -14 = '14 and younger'
15 - high='15 and older'
;
run;

proc means data=sashelp.class;
   class age;
   format age classage.;
   var height weight;
run;

We get the summary statistics of n, mean, standard deviation, minimum and maximum, the defaults for proc means, of the variables height and weight grouped by the age groups.

If I have another format for ages (my permanent format libraries actually have about 12 as different projects use different age ranges for different purposes) I can change the analysis by changing the format.

 

I will say that since I learned SAS before using SPSS for 6 years that I found the whole value label approach clumsy as you had to rebuild the data set in effect every time you needed to modify the labels.

emaguin
Quartz | Level 8

Thank you for your reply and the detailed description you provided.

May i ask, first of all, if you are familiar with those two spss commands, in terms of how the output of frequencies and crosstabs differ when they are not used and used?

 

Here's what i did

 data stack1; set couse.co_usevars;
 label dalc1r = 'Alcohol'
 dmj1r = 'Marijuana'
 dsa1 = 'Went out'
 dsa2 = 'Went where'
 run;

 proc format;
 value dalc1r 1 = 'Yes';
 value dmj1r 1 = 'Yes';
 value dsa1 1 = 'Yes';
 value dsa2 1 = 'Friends house' 2 = 'Someone elses house' 3 = 'Party'
 4 = 'Bar' 5 = 'Other place';
 run;

proc freqs data=stack1;

tables dalc1r dmj1r dsa1 / missprint;

tables dsa1*(dalc1r dmj1r) / missprint nopercent;

run;

I did the labels and proc format using Cody's book  

What wanted for for people to be able to look at the frequency tables and the crosstabs and know three things: variable name(s), what the variable(s) were about (the labels, i.e., variable labels), counts and percentages for data value or combination of data values (the proc formats, i.e., value labels).

Lets start with the labels because proc formats is a much bigger can of worms. What i want to believe is that they, the labels, are like variable labels. They didn't print on the frequencies or crosstabs. Did i do something wrong or are they for some other purpose and were never intended to be printed for that proc? 

Thanks, Gene Maguin

 

 

 

 

 

 

 

Patrick
Opal | Level 21

In SAS a label is for printing the name of a variable, a format for printing values stored in a variable.

Many procedures also allow to use the formats for grouping - meaning you don't have to change the internal value of the variable. 

You can either permanently attach formats and labels to variables or you can specify them within the scope of the Proc only.

Permanently assigned labels and formats are part of the descriptor portion of a SAS dataset ("the header"). You don't need to process the whole data portion to attache them (which a SAS datastep does). Proc Datasets allows you to directly modify the descriptor portion of the SAS dataset.

 

Proc Freq is a very useful procedure but when it comes to creation of reports then consider using Proc Report or eventually Proc Tabulate as these procedures give you more options how you present the data.

data have;
  length dalc1r dmj1r dsa1 dsa2 8;
  do dalc1r=1 to 5;
    dmj1r=dalc1r;
    dsa1=dalc1r;
    dsa2=dalc1r;
    output;
    do dsa2=1,3;
      output;
    end;
  end;
  stop;
run;

proc format;
  value dalc1r_ 
    1-3   = 'Yes'
    other = 'No'
    ;
  value dmj1r_ 
    1,3,4 = 'Yes'
    other = 'No'
    ;
  value dsa1_ 
    1     = 'Yes'
    other = 'No'
    ;
  value dsa2_ 
    1 = 'Friends house' 
    2 = 'Someone elses house' 
    3 = 'Party'
    4 = 'Bar' 
    other = 'Other place'
    ;
run;

proc datasets lib=work nolist;
  modify have;
    label 
      
      dmj1r   = 'Marijuana'
      dsa1    = 'Went out'
      dsa2    = 'Went where'
      ;
    format
      dalc1r  dalc1r_.
      dsa1    dsa1_.
      dsa2    dsa2_.
      ;
  run;   
quit;

proc freq data=have;
  label dalc1r  = 'Alcohol';
  format dmj1r dmj1r_.;
  tables dalc1r dmj1r dsa1 / missprint;
  tables dsa1*(dalc1r dmj1r) / missprint nopercent;
run;

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1068 views
  • 0 likes
  • 3 in conversation