BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
akimme
Obsidian | Level 7

Hi, I'm really new and trying to get SAS to show labels in Results. I'm using SODA, not desktop. I'm not getting any errors but my PROC FORMAT value labels are nowhere in sight. My best guess is that the steps are in the wrong order since otherwise it looks exactly like this example. Can anyone give me some pointers? Thanks so much!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mbuchecker
Quartz | Level 8

Hi Akimme,
The main problem is that you need a format statement to associate your format that you created with the variable name. For example: format MARRSTAT MARRSTAT. ; (notice the period after the name of the format). Format names that you create with PROC FORMAT can have any name that you want , and you can apply that format to more than one variable. That's why the format statement says which variable to apply that format to.
Now some smaller problems, you should move the PROC FORMAT step to the beginning of your program and put the FORMAT statement I have typed above into the first DATA step. Secondly, you can put your LABEL statements in that first data step as well, and then eliminate the second data step. There is no need to re-read the data set just to give it labels.

Michelle

View solution in original post

8 REPLIES 8
mbuchecker
Quartz | Level 8

Hi Akimme,
The main problem is that you need a format statement to associate your format that you created with the variable name. For example: format MARRSTAT MARRSTAT. ; (notice the period after the name of the format). Format names that you create with PROC FORMAT can have any name that you want , and you can apply that format to more than one variable. That's why the format statement says which variable to apply that format to.
Now some smaller problems, you should move the PROC FORMAT step to the beginning of your program and put the FORMAT statement I have typed above into the first DATA step. Secondly, you can put your LABEL statements in that first data step as well, and then eliminate the second data step. There is no need to re-read the data set just to give it labels.

Michelle
akimme
Obsidian | Level 7

Okay, I think I found my problem. I thought that the text after Value was the name of the variable that the format was being applied to, but it's the name of the format itself. A lot of coders that use format names similar to variable names (Insurance2 and Insurance2_) so I got them confused. Thanks also for your tip about combining Data steps. Let me see if that works. Thanks for your help!

Shmuel
Garnet | Level 18

1) Move the proc format to run first.

2) Assign the format name to the appropriate variable, like

     

data ...;
   ....
 format <variable name> <format name>. ; /* don't forget to adjust the dot to the end of the format name */

run;
akimme
Obsidian | Level 7

Oh, so it goes under Data and not Proc Format. Thank you!

tarheel13
Rhodochrosite | Level 12
DATA Marriage;SET Marriage;
LABEL WOMAN ="Woman's CHDS Number";
LABEL MARRSTAT="Marital Status"; /*attempt to label the variables*/
LABEL GATTPREG ="Gravida's Attitude Toward Pregnancy";
LABEL HATTPREG ="Husband's Attitude Toward Pregnancy";
LABEL GR_ATTITUDE ="Dichotomous Gravida's Attitude";
LABEL HB_ATTITUDE ="Dichotomous Husband's Attitude";
RUN;

You don't have to write label every single time. You can just do this: 

DATA Marriage;SET Marriage;
LABEL  WOMAN  ="Woman's CHDS Number";
MARRSTAT="Marital Status"; /*attempt to label the variables*/
GATTPREG  ="Gravida's Attitude Toward Pregnancy";
HATTPREG  ="Husband's Attitude Toward Pregnancy";
GR_ATTITUDE  ="Dichotomous Gravida's Attitude";
HB_ATTITUDE ="Dichotomous Husband's Attitude";
RUN;

 You didn't use a format statement so the formats aren't displaying. Also, it's better programming practice to keep all the formats at the beginning of the program. Personally, I also would not name the format the same as the variable. I would have named it marrstatf or something. 

PROC FREQ data = Marriage;
format marrstat marrstat.;
Table MARRSTAT;
RUN;
akimme
Obsidian | Level 7

Okay, so I've been able to label my Marriage Status variable but it only shows the results if I put those two lines of code under Data, nor Proc Freq. It's not showing my Attitude variables at all, even though I coded them in the exact same way. And I'm getting ERROR 180-322: Statement is not valid or it is used out of proper order. Any insights on that part? I marked it solved it too early!

 

Spoiler

 

filename in "/home/u59281773/sasuser.v94/libref/MARR500.DAT.txt" ;    

PROC FORMAT;
    VALUE MARRSTAT. /*attempt to label the possible values of the variables*/
      1 = "Married"
      2 = "Separated"
      3 = "Divorced"
      4 = "Widowed"
      5 = "Never Married";
VALUE ATTITUDE.
0 = "Strongly Favorable"
1 = "Other";
      RUN;
      
DATA Marriage ;                                                                                                                        
    infile in ;                                                                                                                        
    input   WOMAN  1-5 
            MARRSTAT 7
            GATTPREG 25-26
            HATTPREG 27-28
            GR_ATTITUDE
            HB_ATTITUDE;
if MARRSTAT >= 6 then MARRSTAT   = .;
if GATTPREG  > 8 then GATTPREG    = .;
if HATTPREG  > 8 then HATTPREG    = .;
if HATTPREG   =0 then  HB_ATTITUDE    = 0; 
else if HATTPREG   = "." then HB_ATTITUDE = "."; /*adjusted for missings*/
else if HATTPREG   > 0 then HB_ATTITUDE = 1;
if GATTPREG  =0 then GR_ATTITUDE    = 0;
else if GATTPREG   ="." then GR_ATTITUDE = ".";
else if GATTPREG   > 0 then GR_ATTITUDE = 1;
/*I was so confused until I realized I'd made a typo and the label names didn't match!*/
FORMAT MARRSTAT MARRSTAT. ;
FORMAT GR_ATTITUDE HB_ATTITUDE ATTITUDE. ;
LABEL  WOMAN  ="Woman's CHDS Number";
   LABEL  MARRSTAT  ="Marital Status"; /*attempt to label the variables*/
   LABEL  GATTPREG  ="Gravida's Attitude Toward Pregnancy";
   LABEL  HATTPREG  ="Husband's Attitude Toward Pregnancy";
   LABEL  GR_ATTITUDE  ="Dichotomous Gravida's Attitude";
   LABEL  HB_ATTITUDE  ="Dichotomous Husband's Attitude";
    put _infile_ ; 
    INFORMAT;
RUN;
      
PROC PRINT DATA=Marriage (OBS=20) label;    
RUN;

PROC FREQ data = Marriage;
FORMAT marrstat marrstat.; /*This is a duplicate of two lines under DATA.*/
FORMAT GR_ATTITUDE HB_ATTITUDE ATTITUDE. ; /* If I delete those, the value labels disappear*/
TABLE MARRSTAT; /*So I don't need both but I don't understand how it's suppose to be coded in under PROC FREQ*/
RUN;

 

 

I tried changing the MARRSTAT. format to MARRFT. but then it just gave me error messages.
Tom
Super User Tom
Super User

You can attach the format to the variable when you create the dataset.   When the format is attached to the variable in the dataset then then future procedures that use that dataset will use that format by default. But you can also use a FORMAT statement in a procedure to specify which format to use during that procedure.   Note that using the format statement in a procedure to attach a format to a variable that the procedure is not going to use will not have any real effect.

* Frequency using default format attached in the dataset;
proc freq data=marriage;
  tables marrstat;
run;

* Frequency using format attached in the procedure ;
proc freq data=marriage;
  tables marrstat;
  format marrstat z5. ;
run;

And if you want to have SAS display the values without any special instructions you use a format statement that only lists variables with no format specification after them.

* Frequency of raw values;
proc freq data=marriage;
  tables marrstat;
  format marstat; 
run;
Tom
Super User Tom
Super User

Values don't have labels.  Labels are attached to variables or datasets.  To change how the values of a variable display you can attach a format to the variable using the FORMAT statement.

 

Generally you would define your formats before reading in the data that will need to use them.

 

You can define the labels and attach the formats in the same data step that reads the values in from the text file.  You can use one LABEL statement to define the labels for multiple variables.  Just place the semi-colon that ends that label statement at the end.

 

When reading the data from the source text do not include calculated variables in the INPUT statement. What is the INPUT statement going to see in the source text to read into those variables?

 

When you have a series of variables that have the same set of possible values that you want displayed in the same way you only need to create one format.  You can then attach that same format to all of the variables you want to have displayed in that way.

 

SAS will evaluate boolean expressions to 1 (TRUE) or 0 (FALSE).  This can make created calculated boolean variables very easy.

 

Try something like this (click on SPOILER to expand).

Spoiler
PROC FORMAT;
  VALUE MARRSTAT
    1 = "Married"
    2 = "Separated"
    3 = "Divorced"
    4 = "Widowed"
    5 = "Never Married"
  ;
  VALUE ATTITUDE
    0 = "Strongly Favorable"
    1 = "Other"
  ;
RUN;

DATA Marriage ;
  infile in ;
  input
    WOMAN  1-5
    MARRSTAT 7
    GATTPREG 25-26
    HATTPREG 27-28
  ;
  if MARRSTAT >= 6 then MARRSTAT= .;
  if GATTPREG  > 8 then GATTPREG= .;
  if HATTPREG  > 8 then HATTPREG= .;
  HB_ATTITUDE = (HATTPREG ne 0);
  GR_ATTITUDE = (GATTPREG ne 0);

  format
    MARRSTAT MARRSTAT.
    HB_ATTITUDE GR_ATTITUDE ATTITUDE.
  ;

  LABEL
    WOMAN  ="Woman's CHDS Number"
    MARRSTAT="Marital Status"
    GATTPREG  ="Gravida's Attitude Toward Pregnancy"
    HATTPREG  ="Husband's Attitude Toward Pregnancy"
    GR_ATTITUDE  ="Dichotomous Gravida's Attitude"
    HB_ATTITUDE ="Dichotomous Husband's Attitude"
  ;
RUN;

PROC PRINT DATA=Marriage (OBS=20) label;
RUN;

PROC FREQ data = Marriage;
  table MARRSTAT;
run;

PS:  Please post text as actual text, not attachments, and definitely not as some attached binary file instead of simple text file.  You can use the Insert Code or Insert SAS Code button on the editor menu bar to get a separate pop-up window where you can paste/edit your text or program text.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 8 replies
  • 1003 views
  • 1 like
  • 5 in conversation