07-11-2017 06:35 AM - edited 07-11-2017 06:47 AM
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.
07-11-2017 07:15 AM
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:
You could then simplify and change best4. to just plain 4. instead.
07-11-2017 07:44 AM
One of the reasons why 'some crap'n variable names should (IMO must) not be used.
07-11-2017 09:39 AM
07-11-2017 09:49 AM - edited 07-11-2017 09:50 AM
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.
07-11-2017 09:55 AM
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.
07-12-2017 11:08 AM
07-12-2017 11:17 AM
The X in bestX. refers to the maximum length of the item (for numerics it includes the . if present) so best4. could be:
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: