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

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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.

katvit
Calcite | Level 5

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? 

Reeza
Super User
You may have leading or trailing spaces somewhere. Make sure to apply a TRIM to your variable before using INPUT.
Tom
Super User Tom
Super User

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.);
katvit
Calcite | Level 5

Thank you!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1304 views
  • 1 like
  • 3 in conversation