BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

Why in the following code the output of frequency in "name" field is not correct.

It shows that there are no missing values for field "name " but actually there are 4 missing value

 

proc format ;
value $missfmt
''="missing"
other="Non Missing";
value missfmt
.="missing"
other="Non Missing";
run;
data have;
set sashelp.class;
if _N_ in (2,5,8,13) then do;
  call missing(of _numeric_);
end;
if _N_ in (5,6,8,12) then do;
  call missing(of _character_);
end;
run;
proc freq  data=have;
format _character_ $missfmt.;
tables _character_/missing missprint nocum nopercent;
format _numeric_ missfmt.;
tables _numeric_/missing missprint nocum nopercent;
run;
5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

This is the case for both Sex and Name (the character variables) because you have to specify your format with a blank " " in the Format Procedure and not a "". So this gives your the desired result

 

proc format ;
value $missfmt
' '="missing"
other="Non Missing";

value missfmt
.="missing"
other="Non Missing";
run;

 

 

Capture.PNG 

Astounding
PROC Star
As part of the format definitions, do you have:

''="missing"

If so, try actually inserting a blank between the quotes:

' '="missing"
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

the reason is because the name field for record 5 has a space.  so it is not null nor is it blank for the name field.

try this code and do a proc

proc format ;
value $missfmt
''="missing"
other="Non Missing";
value missfmt
.="missing"
other="Non Missing";
run;
data have;
set sashelp.class;
if _N_ in (2,5,8,13) then do;
  call missing(of _numeric_);
end;
if _N_ in (5,6,8,12) then do;
  call missing(of _character_);
end;
if substr(name,1,1)=" " then notblank = 1;
run;
proc freq  data=have ;
format _character_ $missfmt.;
tables _character_/missing missprint nocum nopercent;
format _numeric_ missfmt.;
tables _numeric_/missing missprint nocum nopercent;
run;
proc freq data=have;
tables notblank / missing;
quit;

freq on the new variable notblank in the dataset have

notblank Frequency
missing 15
Non Missing 4
notblank Frequency Percent Cumulative
Frequency
Cumulative
Percent
. 15 78.95 15 78.95
1 4 21.05 19 100.00
Tom
Super User Tom
Super User

Use the FMTLIST option to PROC FORMAT to see what format you defined.

image.png

Because you did not include anything inside the quotes in your Value statement you ended up with a format for a string that consists of a single quote character instead of one for a blank string.

value $missfmt
  ' '='missing'
  other="Non Missing"
;
ChrisNZ
Tourmaline | Level 20

@Tom meant

Use the FMTLIB option to PROC FORMAT to see what format you defined.

not

Use the FMTLIST option to PROC FORMAT to see what format you defined.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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