Write and run SAS programs in your web browser

Invalid outcome after changing character values to numeric values

Reply
Occasional Contributor
Posts: 6

Invalid outcome after changing character values to numeric values

[ Edited ]

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
Super User
Super User
Posts: 7,401

Re: No outcome after changing character values to numeric values

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

Occasional Contributor
Posts: 6

Re: No outcome after changing character values to numeric values

Sorry for the late upload of the content, @RW9. Now, the content is available. Please advice me.
Super User
Posts: 5,083

Re: Invalid outcome after changing character values to numeric values

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.

Super User
Posts: 6,939

Re: Invalid outcome after changing character values to numeric values

One of the reasons why 'some crap'n variable names should (IMO must) not be used.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 6

Re: Invalid outcome after changing character values to numeric values

@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.
Super User
Posts: 6,939

Re: Invalid outcome after changing character values to numeric values

[ Edited ]

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,401

Re: Invalid outcome after changing character values to numeric values

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.

Occasional Contributor
Posts: 6

Re: Invalid outcome after changing character values to numeric values

Dear @KurtBremser 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.
Super User
Super User
Posts: 7,401

Re: Invalid outcome after changing character values to numeric values

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

Ask a Question
Discussion stats
  • 9 replies
  • 324 views
  • 4 likes
  • 4 in conversation