DATA Step, Macro, Functions and more

convert numeric variable missing values to character variable missing values

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 124
Accepted Solution

convert numeric variable missing values to character variable missing values

Hi,

I used an array to convert the numeric variables  to character variables. None of the numeric variables had any formats associated with it but all had a length of 8. The value for non-missing numeric values was 1 and the value of missing numeric values was a period. I applied the character fomat $40. as I need to reformat the values later. However, after conversion of the numeric to character the missing values are being retained as periods and not blanks. Is there a way to have them as blanks after the conversion? Please let me know as soon as possible. Thanks.

Array _numeric(*)

var2

var3

var4

var5

var6;

 

Array _character(*) $ 40

var2_C

var3_C

var4_C

var5_C

var6_C

DO i=1 to dim(_numeric);

_character(i) = PUT(_numeric(i),8.);

END;


Accepted Solutions
Solution
‎05-15-2017 01:54 AM
Super User
Posts: 5,516

Re: convert numeric variable missing values to character variable missing values

A simple change to one statement:

 

if _numeric(i) > . then _character(i) = PUT(_numeric(i),8.);

View solution in original post


All Replies
Solution
‎05-15-2017 01:54 AM
Super User
Posts: 5,516

Re: convert numeric variable missing values to character variable missing values

A simple change to one statement:

 

if _numeric(i) > . then _character(i) = PUT(_numeric(i),8.);

Respected Advisor
Posts: 3,156

Re: convert numeric variable missing values to character variable missing values

Posted in reply to Astounding

@Astounding, chance is very slim, yet possible:

 

data test;
	input num;
	char=put(num,8.);
	char_1=ifc(num>.,put(num,8.),'');
	char_2=ifc(not missing(num),put(num,8.),'');
	cards;
100
.
._
.A
.Z
;
Super User
Super User
Posts: 7,076

Re: convert numeric variable missing values to character variable missing values

[ Edited ]

You can change the setting of the MISSING option.  Also be careful how you convert to character. Your current use of the PUT() function will cause your character variables to have leading spaces.

2183  %let save=%sysfunc(quote(%qsysfunc(getoption(missing))));
2184  options missing=' ';
2185  data _null_;
2186    do x=0,1,.,123.456 ;
2187      c1=put(x,8.);
2188      c2=cats(x);
2189      put x= (c1 c2) (= :$quote.) ;
2190    end;
2191  run;

x=0 c1="       0" c2="0"
x=1 c1="       1" c2="1"
x= c1="" c2=""
x=123.456 c1="     123" c2="123.456"
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


2192  options missing=&save;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 274 views
  • 2 likes
  • 4 in conversation