BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rk7
Obsidian | Level 7 rk7
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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).

PaigeMiller
Diamond | Level 26

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
rk7
Obsidian | Level 7 rk7
Obsidian | Level 7

can we use the converted variable as character in further statements

rk7
Obsidian | Level 7 rk7
Obsidian | Level 7
 
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):(Column).
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
 
Astounding
PROC Star

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;

rk7
Obsidian | Level 7 rk7
Obsidian | Level 7

Thanks

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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