Hello,
data have;
infile datalines;
input SEQNO Source $ V1 $15.;
datalines;
2 F008 10
2 F008 15
2 F008 20
2 F194 .
;
run;
I want to convert V1 from character to numeric.
The following code does not work
DATA WORK.want (keep=FD_SEQ FD_YEAR FD_QNUM FD_VALUE);
SET WORK.have;
FD_SEQ = SEQNO;
FD_YEAR = 2019;
FD_QNUM = LOWCASE(Source);
FD_VALUE = input(V1, ??30.);
RUN;
What am I doing wrong?
If you transpose character and numeric variables then the resulting variable(s) will be character. The numeric values will be converted to text and RIGHT aligned. You should be able to see this if you look at the data directly or print it to plain old text output. But if you look at it with ODS output the leading spaces will be hidden from you.
Remove the leading spaces.
number=input(left(character),32.);
Your code works fine for me.
data have;
infile datalines;
input SEQNO Source $ V1 $15.;
datalines;
2 F008 10
2 F008 15
2 F008 20
2 F194 .
;
data want;
SET have;
FD_SEQ = SEQNO;
FD_YEAR = 2019;
FD_QNUM = LOWCASE(Source);
FD_VALUE = input(V1, ??30.);
RUN;
proc print;
run;
Obs SEQNO Source V1 FD_SEQ FD_YEAR FD_QNUM FD_VALUE 1 2 F008 10 2 2019 f008 10 2 2 F008 15 2 2019 f008 15 3 2 F008 20 2 2019 f008 20 4 2 F194 2 2019 f194 .
Show the lines from the SAS log for the data step. Make sure to use the Insert Code button (looks like < / > ) to get a pop-up window where you can paste/edit the lines of text you copy from the SAS log.
Remove the ?? modifier to get SAS to spit out notes/errors when the value of V1 is not able to be converted.
Also make sure the character strings are in the first 30 bytes of the V1 values.
I've done some more trouble shooting and I guess I should add more details. The code is not working on a transposed dataset. the original data set looks like this
data have;
infile datalines;
input SEQNO F001 F002 $ F003 $ F004 $ ;
datalines;
1 2 3 text ...
2 3 4 . ...
3 4 5 . ...
2 5 6 . ...
;
I transpose it by using the following
proc transpose data=work.have;
out=work.have
prefix=V
name=Source
label=Label;
by SEQNO;
var &f_name;
RUN;
quit;
After the transpose I get the table I originally provided and the code does not work anymore.
Any additional insights?
If you transpose character and numeric variables then the resulting variable(s) will be character. The numeric values will be converted to text and RIGHT aligned. You should be able to see this if you look at the data directly or print it to plain old text output. But if you look at it with ODS output the leading spaces will be hidden from you.
Remove the leading spaces.
number=input(left(character),32.);
Thank you!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.