BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Annie_Fréchette
Obsidian | Level 7

Hi, I've read some posts and watch the tutorial, but I can figure out how to resolver my (pretty simple) problem. My original data are character format $105 (even if there are numbers) and I want to create a new variable where they would be numerics... Can you help me ? I'm confused between the input, the informat and format... 

/*changer mon type de colonnes, ce sont toutes des character sauf No_ferme*/
data prx;
set pr;
DIMx=input(DIM, $8.);
run;

Here is a print screen of the data and the code.... THANKS!:)

 

Annie_Fréchette_0-1595442588649.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Good that it works.  A couple of points.

There is no need to limit your INFORMAT to just 8 characters.  The maximum width that particular informat supports is 32 and the INPUT function does not mind if the string being converted is shorter than the informat width.

If your variable has length 105 are you sure the digits you want to read are not preceded by spaces that would force them beyond the 8th (or 32nd) character in the string?  If might reduce risk to use LEFT() function to remove any leading spaces.

DIMx=input(left(DIM), 32.);

View solution in original post

7 REPLIES 7
su35
Obsidian | Level 7
The informat means the way you want to read the data. So, you want to input the DIM as numeric, the informat should be "8.". let see DIMx=input(DIM,8.)
Annie_Fréchette
Obsidian | Level 7

There is no bug with this code

data prx;
set pr;
DIMx=input(DIM, 8.);
run;

but my new variable is a "." for every observation....

Annie_Fréchette_0-1595508340284.png

 

Thanks

 

Tom
Super User Tom
Super User

Good that it works.  A couple of points.

There is no need to limit your INFORMAT to just 8 characters.  The maximum width that particular informat supports is 32 and the INPUT function does not mind if the string being converted is shorter than the informat width.

If your variable has length 105 are you sure the digits you want to read are not preceded by spaces that would force them beyond the 8th (or 32nd) character in the string?  If might reduce risk to use LEFT() function to remove any leading spaces.

DIMx=input(left(DIM), 32.);
Annie_Fréchette
Obsidian | Level 7

Thanks! With that code, it is working!:)

mklangley
Lapis Lazuli | Level 10

Just change the informat from $8. to 8., since the string you're converting is a string of numbers.

data prx;
set pr;
DIMx=input(DIM, 8.);
run;
mkeintz
PROC Star

Just to take home the primary message in the replies from @mklangley and @su35:  specify informats in an INPUT function reading a character variable exactly as you specify informats in an INPUT statement reading data from a text file with the same content.  

 

So, since you would use an informat of 8. reading numerics from the external file, use it in the input function too.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
andreas_lds
Jade | Level 19

You should check why the information is stored in a char-variable, at all. And why its length is 105 chars, as soon as the number has more than 15 digits you will loose precision when converting it to numeric.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1227 views
  • 1 like
  • 6 in conversation