BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ANKH1
Pyrite | Level 9

Hi, how do you convert a variable type number to character when some values include special characters such <? e.g., p_value=<.0001. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

In that case just do it yourself.

if not missing(numvar) then do;
  if numvar >= 0.0001 then charvar=put(numvar,7.4);
  else charvar='<0.0001';
end;

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

@ANKH1 wrote:

Hi, how do you convert a variable type number to character when some values include special characters such <? e.g., p_value=<.0001. 


Did you mix up the direction of the conversion?  If the value is numeric it cannot have any special characters.  

 

So assuming you want to convert a character string to a number you will have to decide what to do with extra characters.  Do you just want to remove them?

So you could remove specific characters:

numvar = input(compress(charvar,'<'),32.);

Or only keep digits and normal punctuation. 

numvar = input(compress(charvar,'+-.','kd'),32.);
ANKH1
Pyrite | Level 9
Hi, the variable comes from an output after running proc glmm. I am getting the output to create a table with results. The output specifies ProbF as a numeric variable. Some of the values are <.0001. The issue is that if I leave the type as numeric I get the actual number e.g., 0.000054 instead of the convention of reporting a pvalue as <0.0001.
Tom
Super User Tom
Super User

In that case just do it yourself.

if not missing(numvar) then do;
  if numvar >= 0.0001 then charvar=put(numvar,7.4);
  else charvar='<0.0001';
end;
FreelanceReinh
Jade | Level 19

@ANKH1 wrote:
Hi, the variable comes from an output after running proc glmm. I am getting the output to create a table with results. The output specifies ProbF as a numeric variable. Some of the values are <.0001. The issue is that if I leave the type as numeric I get the actual number e.g., 0.000054 instead of the convention of reporting a pvalue as <0.0001.

Hi @ANKH1,

 

You can get the formatted value with the VVALUE function, even without knowing the format. You should specify a reasonable length, though:

data want;
set have;
length char_ProbF $8;
char_ProbF=vvalue(ProbF);
run;

 

Astounding
PROC Star
The values like < .0001 are already numeric. They are printing using a format that includes a less than sign for small values.

One choice you have is to remove the format before printing:
format var;

Another choice you have is to change the data:
if var < .0001 then var = .0001;
Does that make sense?

A_Kh
Lapis Lazuli | Level 10

Do not convert it into character, better apply format pvalue6.4 which displays the value as expected

data have; 
set have; format probF pvalue6.4;
run; 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1407 views
  • 2 likes
  • 5 in conversation