DATA Step, Macro, Functions and more

format to convert nueric to character

Accepted Solution Solved
Reply
Contributor rk7
Contributor
Posts: 28
Accepted Solution

format to convert nueric to character

Hi I have a problem converting numeric to character using put statement . It doesn't gets converted and displays error. can u pls tell me what format to use to convert numeric to character and also give the list of all formats and informants


Accepted Solutions
Solution
‎11-28-2017 02:56 PM
Super User
Posts: 6,935

Re: format to convert nueric to character

If you want help with the proper syntax, you will need to show the log from what you have tried.

 

Here is an example of the most common error when converting from numeric to character:

 

data want;

set have;

var = put(var, z5.);

run;

 

The PUT function really works .... it converts the value of VAR to a character string.  However, you can't store the character string in VAR, since VAR is already defined as numeric.  The PUT function cannot change an existing variable from being numeric to being character.  Instead, you need to create a new variable:

 

newvar = put(var, z5.);

 

That may or may not be your error, but it is a very common error.  

 

As for a list of all formats and informats ... there are hundreds of them.  You will have to search the documentation for those, although this is a decent starting point:

 

http://documentation.sas.com/?docsetId=allprodslang&docsetTarget=syntaxByType-format.htm&docsetVersi...

 

You won't need to know about informats, since they don't convert from numeric (only from character).

View solution in original post


All Replies
Solution
‎11-28-2017 02:56 PM
Super User
Posts: 6,935

Re: format to convert nueric to character

If you want help with the proper syntax, you will need to show the log from what you have tried.

 

Here is an example of the most common error when converting from numeric to character:

 

data want;

set have;

var = put(var, z5.);

run;

 

The PUT function really works .... it converts the value of VAR to a character string.  However, you can't store the character string in VAR, since VAR is already defined as numeric.  The PUT function cannot change an existing variable from being numeric to being character.  Instead, you need to create a new variable:

 

newvar = put(var, z5.);

 

That may or may not be your error, but it is a very common error.  

 

As for a list of all formats and informats ... there are hundreds of them.  You will have to search the documentation for those, although this is a decent starting point:

 

http://documentation.sas.com/?docsetId=allprodslang&docsetTarget=syntaxByType-format.htm&docsetVersi...

 

You won't need to know about informats, since they don't convert from numeric (only from character).

Respected Advisor
Posts: 3,279

Re: format to convert nueric to character

Posted in reply to Astounding

In addition to what Astounding said, formats do not convert variables from numeric to character. Formats DISPLAY (not convert) numeric variables as character. Applying a format to a numeric variable means that the variable remains numeric, but may be displayed as a set of characters.

 

As @Astounding points out, the PUT statement can create a new variable with a new name which is a different type.

--
Paige Miller
Contributor rk7
Contributor
Posts: 28

Re: format to convert nueric to character

Posted in reply to PaigeMiller

can we use the converted variable as character in further statements

Contributor rk7
Contributor
Posts: 28

Re: format to convert numeric to character

Posted in reply to Astounding
 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62 data stars;
63 input sbp dbp @@;
64 length sbp_chk dbp_chk 4.;
65 sbp_chk= put(sbp,3.);
66 dbp_chk= put(dbp,3.);
67 if sbp>120 then
68 substr(sbp_chk, 4, 1)='*';
_
356
ERROR: Argument to SUBSTR must be character.
NOTE 356-185: The SUBSTR pseudo-variable function does not allow character constants, expressions, or numeric constants for the
first argument.
 
69 if dbp>80 then
70 substr(dbp_chk, 4, 1)='*';
_
356
ERROR: Argument to SUBSTR must be character.
NOTE 356-185: The SUBSTR pseudo-variable function does not allow character constants, expressions, or numeric constants for the
first argument.
 
71 datalines;
 
NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
65:11 66:11
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.STARS may be incomplete. When this step was stopped there were 0 observations and 4 variables.
WARNING: Data set WORK.STARS was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
73 ;
 
 
74 run;
75
76 proc printto print='/folders/myfolders/listoutputs/f3_1stars.lst' new;
77 run;
 
NOTE: PROCEDURE PRINTTO used (Total process time):
real time 0.04 seconds
cpu time 0.03 seconds
 
 
78
79 proc print ;
80 run;
 
NOTE: There were 3 observations read from the data set WORK.STARS.
NOTE: The PROCEDURE PRINT printed page 3.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds
 
 
81
82 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
95
 
Super User
Posts: 6,935

Re: format to convert numeric to character

Your LENGTH statement is defining the new variables as numeric, when you need them to be character.  Try it this way:

 

length sbp_chk dbp_chk $4;

Contributor rk7
Contributor
Posts: 28

Re: format to convert numeric to character

Posted in reply to Astounding

Thanks

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 185 views
  • 1 like
  • 3 in conversation