Hello, I have a invalue format to populate the value of VISITNUM for some records where it's value is missing using AVISITN which is populated for all records as a reference. Both are numeric variables. So if AVISITN = 10 I would want the missing VISITNUM to be 1 etc.
data have;
input VISITNUM :8. AVISITN :8.;
infile datalines dlm = '|';
datalines;
1|10|
2|20|
4|30|
5|40|
6|50|
7|60|
8|70|
1|10|
2|20|
4|30|
5|40|
6|50|
|60|
|70|
|80|
1|10|
2|20|
4|30|
5|40|
|50|
|60|
|70|
|80|
1|10|
2|20|
|30|
|40|
|50|
|60|
|70|
|80|
;
RUN;
data want;
input VISITNUM :8. AVISITN :8.;
infile datalines dlm = '|';
datalines;
1|10|
2|20|
4|30|
5|40|
6|50|
7|60|
8|70|
1|10|
2|20|
4|30|
5|40|
6|50|
7|60|
8|70|
9|80|
1|10|
2|20|
4|30|
5|40|
6|50|
7|60|
8|70|
9|80|
1|10|
2|20|
4|30|
5|40|
6|50|
7|60|
8|70|
9|80|
;
RUN;
proc format;
invalue dummy_visnum
10 = 1
20 = 2
30 = 4
40 = 5
50 = 6
60 = 7
70 = 8
80 = 9
100= 10;
quit;
However when I run this code it works as intended but I get a warning in my log "Numeric values have been converted to character values at the places given by: (Line):(Column)."
visitnum = input(avisitn, dummy_visnum.);
What is the workaround to eliminate this?
FORMATS convert values to text. INFORMATS convert text to values.
Numeric formats convert numbers to text.
Numeric informats convert text to numbers.
Character formats AND character informats convert text to text.
There is not number to number conversion.
Use a different method. Use both PUT() and INPUT().
new_number = input(put(old_number,myformat.),32.);
Use a look-up table. You could use an INDEX.
data want;
set have ;
set lookup index=old_number;
run;
Or a hash object.
"The VALUE statement in PROC FORMAT is used to define a FORMAT. The INVALUE statement is used to define an INFORMAT. In SAS you use a FORMAT to convert values into text and an INFORMAT to convert text into values."
With INFORMAT, you are telling SAS that AVISITN Is text, but it is actually a number. Hence, SAS converts AVISITN into text.
Why not just divide by 10?:
if missing(visitn) then visitn = avisitn / 10
Yeah I would do that normally but the data is slightly asynchronous as it does not have a one to one conversion as visitnum = 3 is AVISITN = 40 and same for VISITNUM 4 to 9. It is only 1,2 and 10 where this would work . Yeah I can see that my approach doesn't work and I've tried other ways such as this
visitnum = input(cats(mod(avisitn,10)),dummy_visnum.);
But I get values of 0 also for the same reason I presume
@smackerz1988 wrote:
Yeah I would do that normally but the data is slightly asynchronous as it does not have a one to one conversion as visitnum = 3 is AVISITN = 40 and same for VISITNUM 4 to 9. It is only 1,2 and 10 where this would work . Yeah I can see that my approach doesn't work and I've tried other ways such as this
visitnum = input(cats(mod(avisitn,10)),dummy_visnum.);
But I get values of 0 also for the same reason I presume
Simple algebra seems to me to be the easiest way to get there, and with the least coding. An IF statement doing one thing when AVISITN if 40 or greater, and an ELSE statement to handle when AVISITN < 40.
of course but it was more the approach to getting around the numeric to numeric format problem moreso than that. I knew it was a combination out put/input but was just missing the 32..
FORMATS convert values to text. INFORMATS convert text to values.
Numeric formats convert numbers to text.
Numeric informats convert text to numbers.
Character formats AND character informats convert text to text.
There is not number to number conversion.
Use a different method. Use both PUT() and INPUT().
new_number = input(put(old_number,myformat.),32.);
Use a look-up table. You could use an INDEX.
data want;
set have ;
set lookup index=old_number;
run;
Or a hash object.
I was missing the 32. at the end!. Thank you!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.