Vixxen81
Vixxen81

Reputation: 33

Proc format and format SAS

Use PROC FORMAT AND FORMAT to create SAS formats for gender variable in the gym data. Print the ID, Gender, and PBar variables such that the formats are given in the output.

I can get everything to print, but the variables aren't printing right. What am I doing wrong?

PROC FORMAT;    VALUE $Gender 'M'='Male'                    
                              'F'='Female';  
data gym; input ID Gender $ PBar; 
datalines; 
3 M 6.5 
5 F 6.8 
2 F 5.8 
7 M 6.1 
6 F 7.9 ; 
format $gender.;  
PROC PRINT data=gym; 
run;quit;

Upvotes: 2

Views: 1250

Answers (4)

Tom
Tom

Reputation: 51621

The main problem is that your FORMAT statement is between the DATA step and the PROC PRINT step. So it will never be applied. Either move it before the DATALINES (aka CARDS) statement so that it is part of the DATA step or move it after the PROC PRINT so that it is part of the PROC step.

Upvotes: 0

akaDrHouse
akaDrHouse

Reputation: 2250

Torm and Vishant gave great advice and options.

The reason yours didn't print though was that your format statement was incomplete.

It should be:

format gender $gender.; *you didn't specify which variable needed formatted*; 
PROC PRINT data=gym; 
run;quit;

Upvotes: 1

Vishant
Vishant

Reputation: 266

There are 2 ways of doing it,
Solution 1

proc format;  
value $gender  
'M'='Male'  
'F'='Female'  
other= 'Unknown'; * Handle Missing Values;    
run;  

data gym; 
infile datalines dlm=' ' dsd missover; *Delimited Dataset handling Missing  values;  
input ID Gender $ PBar; 
/* Format Statement here, the variable name was missing in your code */  
format gender $gender.; 
datalines;   
3 M 6.5   
5 F 6.8   
2 F 5.8   
7 M 6.1   
6 F 7.9   
;  
run;   
proc print data=gym;   
run;  

In this solution the dataset has the embedded format. So irrespective of the print statement you will see the format applied.

Solution 2 as as mentioned by @torm which will apply the format while you print and disappears when the print procedure completes.
So choose your option as per what you need.

Upvotes: 0

torm
torm

Reputation: 1536

You need to use a format statement inside a proc print. This code works:

PROC FORMAT; VALUE $Gender 'M'='Male' F'='Female';  

data gym; 
    input ID Gender $ PBar; 
    datalines; 
    3 M 6.5 
    5 F 6.8 
    2 F 5.8 
    7 M 6.1 
    6 F 7.9 
; *remember about proper syntax here;

proc print data=gym;
    format Gender $gender.;
run;

Upvotes: 0

Related Questions