DATA Step, Macro, Functions and more

converting numeric to character by put statement: how to avoid "." for missing values

Accepted Solution Solved
Reply
Super Contributor
Posts: 318
Accepted Solution

converting numeric to character by put statement: how to avoid "." for missing values

 

when I use put statement to convert a numeric variable to character one, using the following code will make missings to '.', not blank. 

VSSTRESC=strip(put(VSRESPORRES,best.));

Then I need to use the following code to avoid '.' 

if VSRESPORRES ne . then  VSSTRESC=strip(put(VSRESPORRES,best.));
				else VSSTRESC='';

I have many such variables to convert. I wonder if there is an easier way to avoid it? 

 


Accepted Solutions
Solution
‎01-03-2017 01:21 PM
Super User
Posts: 19,861

Re: converting numeric to character by put statement: how to avoid "." for missing values

If you're lazy, you can use CATT, it does an automatic conversion, with no message in the log.

 

option missing='';

data class;
	set sashelp.class;

	if age=13 then
		weight=.;
run;

data check;
	set class;
	test1=catt(weight);
	test2=put(weight, best.);
run;

title 'Missing with space';

proc print data=check (obs=5);
run;

option missing=.;
title 'Missing with period';

proc print data=check(obs=5);
run;

 

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,584

Re: converting numeric to character by put statement: how to avoid "." for missing values

Posted in reply to fengyuwuzu

add options missing=' '; before your step

Super Contributor
Posts: 318

Re: converting numeric to character by put statement: how to avoid "." for missing values

Thank you. Will this option statement affect the missing values of numeric varaibles in the same data step? 

Trusted Advisor
Posts: 1,584

Re: converting numeric to character by put statement: how to avoid "." for missing values

Posted in reply to fengyuwuzu

the option options missing=' '; affects only how it is displayed.

Respected Advisor
Posts: 4,173

Re: converting numeric to character by put statement: how to avoid "." for missing values

Posted in reply to fengyuwuzu

As an alternative to the missing option which affects how missings are printed for all numerical variables, you can also define your own format as demontrated in the code below.

 

Also: To get your result left aligned you could use syntax <format name>.-L 

 

proc format;
  value MyBest(default=16)
  low-high=[best.]
  other=' ';
run;

data sample;
  VSRESPORRES=1;
  VSSTRESC=put(VSRESPORRES,MyBest.-L);
  output;
  VSRESPORRES=.;
  VSSTRESC=put(VSRESPORRES,MyBest.-L);
  output;
run;
Solution
‎01-03-2017 01:21 PM
Super User
Posts: 19,861

Re: converting numeric to character by put statement: how to avoid "." for missing values

If you're lazy, you can use CATT, it does an automatic conversion, with no message in the log.

 

option missing='';

data class;
	set sashelp.class;

	if age=13 then
		weight=.;
run;

data check;
	set class;
	test1=catt(weight);
	test2=put(weight, best.);
run;

title 'Missing with space';

proc print data=check (obs=5);
run;

option missing=.;
title 'Missing with period';

proc print data=check(obs=5);
run;

 

 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 392 views
  • 0 likes
  • 4 in conversation