BookmarkSubscribeRSS Feed
kjyon2
Calcite | Level 5

Dear SAS users,

 

I have studied the informat topic and used the INPUT() function to change a character variable (STDs:Year_since_first_diagnosis) into a numeric (STD_1stDx). The outcome of the execution of code is shown below. However, the outcome of the numeric STD_1stDx contains no observation at all. Initially, I thought the BEST.w format is incorrect, but the missing observation is still persist even after I have changed it from best4. to best12..

 

Data work.cervical_cancer_t2;
set work.import;
	STD_1stDx = input('STD:Year_since_first_diagnosis', best4.);
RUN;

 

My main concerns are:

(1) I am still confused about the proper usage of BEST.w in this case for converting character to numeric and vice versa.

(2) What steps can I do now to get the proper observation in the STD_1stDx, so that I can first perform descriptive analysis and subsequently deal with the missing values?

 

I have attached the excel file for the output of the observation, and the output of the compiled Proc Contents.

 

Thank you so much.


variable_char to num.png
9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Missed something from your post...the content perhaps?

kjyon2
Calcite | Level 5
Sorry for the late upload of the content, @RW9. Now, the content is available. Please advice me.
Astounding
PROC Star

You're missing just a single character:  n

 

If you have a variable named STD:Year_since_first_diagnosis, you have to refer to that variable as:

'STD:Year_since_first_diagnosis'n

 

 

You could then simplify and change best4. to just plain 4. instead.

kjyon2
Calcite | Level 5
@RW9 and KurtBremser, do you mean that HOW the variable name is being assigned/named can affect the ability of SAS to correctly change the variable type from Character to Numeric? Can this change made disallow the missing observations to be done? Please understand my questions and advise me with explanations. Thank you so much.
Kurt_Bremser
Super User

In your code, you tried to use a non-standard SAS name, but failed to append the n after the name string:

STD_1stDx = input('STD:Year_since_first_diagnosis', best4.);

it should have been

STD_1stDx = input('STD:Year_since_first_diagnosis'n, best4.);

So a very little omission (that doesn't even cause a syntax ERROR message!) caused missing values, because SAS tried to convert the string 'STD:Year_since_first_diagnosis' to numeric instead of the contents of the variable named STD:Year_since_first_diagnosis.

By using standard valid SAS names, one is notfied of a typo immediately because a string without quotes HAS to be a variable name.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to add, having '<string>'n is called named literals and it is a method of refering to external files/databases which allow for variable names to not conform to SAS standards, typically when using an Excel file you will see this as Excel allows anything in its cells.  For normal programm activities it is better to import the data intoa dataset, and then use the actual names of variables rather than keep refering to the named literals as this both makes your code simpler, easier to read, and avoids these complications arising from the use of named literals.

kjyon2
Calcite | Level 5
Dear @Kurt_Bremser and @RW9, may I know what informat and format of the bestx., where x is a number, should I use for the input and put functions when I convert my variable from character to numeric, and vice versa? What about the best12. and best32.? Please provide me with some explanations. Thank you.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The X in bestX. refers to the maximum length of the item (for numerics it includes the . if present) so best4. could be:

123

1.23

12.3

 

Without knowing your data we cannot possible tell you what format best applies to the data only you can see or know about.

 

I would also suggest running through the tutorials and such like that SAS provide as this is a basic fundamental of SAS:

http://video.sas.com/#category/videos/how-to-tutorials

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
  • 9 replies
  • 1284 views
  • 4 likes
  • 4 in conversation