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

i want to change variable MSRP which is in char format to numeric type but i dont know how to do it.

any help with complete syntex .

my code is :-


data cars1;
set work.cars;
input MSRP 10.;
fmtMSRP=input(MSRP,comma10.);
run;

 

here the data set was cars where my MSRP is in char format but i wish to convert it into numeric format.

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

You can use INPUT either with INFILE or as a function.

To convert the char type to numeric you need a mid name:

data cars1;
  set work.cars(rename=(MSPR=_mspr));  /* <<< added closing ')' */
        mspr = input(_mspr,dolar10.);
        drop _mspr;
        fmtMSRP=input(MSRP,dolar10.);
run;

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

If MSRP is character then

 

msrp_numeric=msrp+0;
--
Paige Miller
Shmuel
Garnet | Level 18

You can use INPUT either with INFILE or as a function.

To convert the char type to numeric you need a mid name:

data cars1;
  set work.cars(rename=(MSPR=_mspr));  /* <<< added closing ')' */
        mspr = input(_mspr,dolar10.);
        drop _mspr;
        fmtMSRP=input(MSRP,dolar10.);
run;
jonty
Fluorite | Level 6
no this aint working it creating the variables but no data in the variables all ate blank
Reeza
Super User
Post your code and log. Also, post a proc contents from your INPUT data set.
jonty
Fluorite | Level 6

AS YOU CAN SEE THE DATA TYPE FOR MSRP IS CHAR BUT I WANT IT IN NUMERIC FOR FURTHER CALCULATION AND I WANT SAME VALUES LIKE $25,214 LIKE THIS BUT IN NUMERIC FORMAT...

 

image.png

 

 

 

code :-

 


data cars1;
set work.cars(rename=(MSRP=_msrp));
msrp = input(_msrp,dollar10.);
run;

 

 

THIS IS GIVING ME OUTPUT BUT NOW I WANT IT TO BE IN DOLLAR FORMAT LIKE $25,254 LIKE THIS BUT REEZA I AM SKIPPING SOMETHING 

Reeza
Super User

APPLY A FORMAT. OTHERWISE THE DEFAULT FORMAT IS BEST OR 8. WHICH HAS NO DOLLAR SIGNS. SO IF YOU WANT DOLLAR SIGNS YOU APPLY THE CORRECT FORMAT WHICH CONTROLS THE DISPLAY. NOTE THAT THE VALUE STORED IS STILL NUMERIC, FORMATS ONLY CONTROL THE DISPLAY OF THE DATA.

 

delete_format.JPG

 

 

jonty
Fluorite | Level 6
I AM SORRY NOT ABLE TO GET IT CAN YOU HELP WITH PROPER SYNTEX
I THICK THAT WILL HELP ME TO UNDERSTAND AND I WANT INPUT STATEMENT TO BE USED
Reeza
Super User

ALL CAPS is yelling on the internet. DON'T DO IT.
https://en.wikipedia.org/wiki/All_caps

 

And I did include the code, the format statement is in my post - add it into your current code. 

You never posted the code/log and proc contents, ergo these circles. 

Shmuel
Garnet | Level 18

I have edited my posted code. It should work.

Shmuel
Garnet | Level 18
See last edited code - changing informat and format to dolar10.
jonty
Fluorite | Level 6
this is working fine btw any clue how to simplify it more ????

data cars1;
set work.cars(rename=(MSRP=MSRP1));
MSRP = input(MSRP1,DOLLAR10.);
FORMAT MSRP DOLLAR10.;
DROP MSRP1;
run;
Reeza
Super User

@jonty wrote:
this is working fine btw any clue how to simplify it more ????




The best way is to read the data in correctly in the first place, but otherwise this is the simplest way to change the variable type.

Reeza
Super User
If you can, you should change the correct answer to @Shmuel post, it has the full correct solution.
jonty
Fluorite | Level 6
OK surely if I can I will I dnt even know how community work in sas
I was not able to accept that as a solution so I likes it
May be I dnt know how to do it still I will try

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 1917 views
  • 4 likes
  • 4 in conversation