hi
sub var
110 new
111 1234
101 char
100 321
in the above table 'var' being a character variable, pls help me to solve
newvar=input(var,best.);?
I think you are looking to use the ?? modifier
data have;
input sub var $;
cards;
110 new
111 1234
101 char
100 321
;
data want;
set have;
newvar=input(var,?? best. );
run;
Var must be character because it has mixed types, character and numeric. What do you want to happen with the character values?
Otherwise your code looks correct, did it not work when you tried it? If so, show what you tried and the error.
@AKHILA wrote:
hi
sub var
110 new
111 1234
101 char
100 321
in the above table 'var' being a character variable, pls help me to solve
newvar=input(var,best.);?
this was an interview qstn.they asked me to write the output for this.
I'm guessing they were expecting you to say this would create an error because of ABC Reason and that the way to read it in is to use an IF/THEN, or IFC/IFN statement or suppress the errors in the INPUT(), but that suppressing errors automatically is dangerous because of XYX.
@AKHILA wrote:
this was an interview qstn.they asked me to write the output for this.
Converting variables between types is a basic task that they would expect an analyst to be familiar with if they state they know SAS.
Do you mean that you have a value that reads "var" or "char" and you want it to become numeric? Since there is no obvious conversion of text "var" or "char" to numeric the value from an input would missing. If you expect a numeric value you will need to provide one elsewhere.
The values 1234 and 321 should convert without any problem with the code that you show.
I think you are looking to use the ?? modifier
data have;
input sub var $;
cards;
110 new
111 1234
101 char
100 321
;
data want;
set have;
newvar=input(var,?? best. );
run;
FORMATS are used to convert values into strings. The BEST format is for converting numbers into strings that will best fit the width provided.
But your program is trying to use the BEST informat instead of the BEST format. Informats are used to convert strings into values. Numeric informats convert strings into numbers. BEST when used as in INFORMAT is just an alias for the normal numeric informat. There is only one way to store an actual number in SAS. There is no "best" way to store a number.
So your program will convert the strings that look like numbers into those numbers. The other strings, like 'var' will be converted into missing values. It will also write notes to the log when it cannot convert the value. You use the ? or ?? prefix on the informat in your INPUT() function call to suppress the error messages.
this was an interview qstn.they asked me to write the output for this.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.