SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Put Function not converting numeric values to character.

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 13
Accepted Solution

Put Function not converting numeric values to character.

Hi SAS Community,

 

I have a variable for patient visit that I want to convert from numeric to character. I've been using the put function to create a new variable and convert the values from the old numeric variable to character values. It works for some of my data sets, but for others it does not (same code and input variable is the same format in each data set). Maybe I'm just over looking a detail, but here is my issue below.

 

(I'm using SAS 9.4 TS1M4 on X64_7Pro platform.)

 

Variable I have: "newvisit", format 11.

Variable I want: "visit2", format $10.

data phru_13ad (keep=studyid issue db sq visit2 variable dataset visitdate form); set phru_13ad1b phru_13ad2b;
	form="CRF13"; variable=" "; sq=" "; db=" "; visitdate=visitdate13; format visitdate date9.;
	dataset="phru_13add"; visit2=put(newvisit,10.);; run;

proc sort data=phru_13ad out=phru_13add nodupkey; by studyid visitdate visit2; run;

Log Reads:

110 data phru_13ad (keep=studyid issue db sq visit2 variable
110! dataset visitdate form); set phru_13ad1b phru_13ad2b;
111 form="CRF13"; variable=" "; sq=" "; db=" ";
111! visitdate=visitdate13; format visitdate date9.;
112 dataset="phru_13add"; visit2=put(newvisit,10.);; run;

NOTE: Character values have been converted to numeric

values at the places given by: (Line)Smiley SadColumn).
112:34
NOTE: There were 1 observations read from the data set
WORK.PHRU_13AD1B.
NOTE: There were 1 observations read from the data set
WORK.PHRU_13AD2B.
NOTE: The data set WORK.PHRU_13AD has 2 observations and 9
variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

 

For some reason it's converting it back into a numeric format after it has been converted to character. Does anyone know why this is happening?

 

Thanks,

Cara


Accepted Solutions
Solution
4 weeks ago
Super User
Posts: 6,018

Re: Put Function not converting numeric values to character.

[ Edited ]

This would happen if VISIT2 is already defined as numeric.  Check the incoming data sets, and see if VISIT2 exists already.  If so, you can get of it easily enough.  For example, if it is part of PHRU_13AD1B:

 

set phru_13ad1b (drop=visit2) phru_13ad2b;

View solution in original post


All Replies
Solution
4 weeks ago
Super User
Posts: 6,018

Re: Put Function not converting numeric values to character.

[ Edited ]

This would happen if VISIT2 is already defined as numeric.  Check the incoming data sets, and see if VISIT2 exists already.  If so, you can get of it easily enough.  For example, if it is part of PHRU_13AD1B:

 

set phru_13ad1b (drop=visit2) phru_13ad2b;

Occasional Contributor
Posts: 13

Re: Put Function not converting numeric values to character.

Posted in reply to Astounding

Hi and thank you - this worked! 

Super User
Posts: 21,596

Re: Put Function not converting numeric values to character.

Does the variable already exist in those data sets? And if it does is the type character?

 


cbt2119 wrote:

Hi SAS Community,

 

I have a variable for patient visit that I want to convert from numeric to character. I've been using the put function to create a new variable and convert the values from the old numeric variable to character values. It works for some of my data sets, but for others it does not (same code and input variable is the same format in each data set). Maybe I'm just over looking a detail, but here is my issue below.

 

(I'm using SAS 9.4 TS1M4 on X64_7Pro platform.)

 

Variable I have: "newvisit", format 11.

Variable I want: "visit2", format $10.

data phru_13ad (keep=studyid issue db sq visit2 variable dataset visitdate form); set phru_13ad1b phru_13ad2b;
	form="CRF13"; variable=" "; sq=" "; db=" "; visitdate=visitdate13; format visitdate date9.;
	dataset="phru_13add"; visit2=put(newvisit,10.);; run;

proc sort data=phru_13ad out=phru_13add nodupkey; by studyid visitdate visit2; run;

Log Reads:

110 data phru_13ad (keep=studyid issue db sq visit2 variable
110! dataset visitdate form); set phru_13ad1b phru_13ad2b;
111 form="CRF13"; variable=" "; sq=" "; db=" ";
111! visitdate=visitdate13; format visitdate date9.;
112 dataset="phru_13add"; visit2=put(newvisit,10.);; run;

NOTE: Character values have been converted to numeric

values at the places given by: (Line)Smiley SadColumn).
112:34
NOTE: There were 1 observations read from the data set
WORK.PHRU_13AD1B.
NOTE: There were 1 observations read from the data set
WORK.PHRU_13AD2B.
NOTE: The data set WORK.PHRU_13AD has 2 observations and 9
variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

 

For some reason it's converting it back into a numeric format after it has been converted to character. Does anyone know why this is happening?

 

Thanks,

Cara


 

Super User
Posts: 12,148

Re: Put Function not converting numeric values to character.

I think you will find that your variable NEWVISIT is already character if you run proc contents or examine the data set columns for data set phru_13ad1b .

This note from your log

NOTE: Character values have been converted to numeric

values at the places given by: (Line):(Column).
112:34

says that something that is already character was treated in a numeric fashion.

 

112 dataset="phru_13add"; visit2=put(newvisit,10.);; run;

Look in your original code at column 34 for the above line. Likely PUT is at position 34. Which tells you what attempted to treat a character variable as numeric.

 

You will get different behaviors if your variables come from data sets created by proc import as each time import runs it makes a guess as to the contents of the file. So sometimes a variable will be character and sometimes not.

 

BTW it is much easier to read and understand code if each line is a single statement.

data phru_13ad (keep=studyid issue db sq visit2 variable dataset visitdate form); 
   set phru_13ad1b phru_13ad2b;
   form="CRF13"; 
   variable=" "; 
   sq=" "; 
   db=" "; 
   visitdate=visitdate13; 
   format visitdate date9.;
   dataset="phru_13add"; 
   visit2=put(newvisit,10.);
run;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 184 views
  • 2 likes
  • 4 in conversation