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

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

8 REPLIES 8
Cynthia_sas
SAS Super FREQ
Hi:
What program have you tried? What happened when you used your INPUT function?
Cynthia
Reeza
Super User

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


 

AKHILA
Obsidian | Level 7

this was an interview qstn.they asked me to write the output for this.

Reeza
Super User

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. 

ballardw
Super User

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.

 

novinosrin
Tourmaline | Level 20

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;
Tom
Super User Tom
Super User

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.

AKHILA
Obsidian | Level 7

this was an interview qstn.they asked me to write the output for this.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 11324 views
  • 8 likes
  • 6 in conversation