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):(Column).
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
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;
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;
Hi and thank you - this worked!
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):(Column).
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
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.