BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JKim8069
Calcite | Level 5
Assuming the ID column in the employees table is numeric, which statement is true?

1 data convert;
2 set employees;
3 ID=put(ID, 5.);
4 run;
5
Answer: The step runs, but ID is not converted to a character column.

My question: I can’t find any reason why ‘ID=put(ID, 5.)’ is wrong….
I appreciate your help.
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You cannot change ID from numeric to character. If ID is numeric in data set EMPLOYEES, then it will be numeric in data set CONVERT as well.

 

To create a character variable, use a different variable name, such as:

 

ID1=put(ID, 5.) 

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

You cannot change ID from numeric to character. If ID is numeric in data set EMPLOYEES, then it will be numeric in data set CONVERT as well.

 

To create a character variable, use a different variable name, such as:

 

ID1=put(ID, 5.) 

 

--
Paige Miller
ballardw
Super User

@JKim8069 wrote:
Assuming the ID column in the employees table is numeric, which statement is true?

1 data convert;
2 set employees;
3 ID=put(ID, 5.);
4 run;
5
Answer: The step runs, but ID is not converted to a character column.

My question: I can’t find any reason why ‘ID=put(ID, 5.)’ is wrong….
I appreciate your help.

‘ID=put(ID, 5.) isn't "wrong". This sort of quiz question to see if you understand the behavior in SAS that a variable doesn't change it's type.

 

One thing that is often revealing is to run code to TEST these types of questions. I know that during a scored quiz that may not be possible but if you do so you may see some revealing items. Running this to create a simple data set:

data employees;
  input id ;
datalines;
12
1234
1234567
;

Then running your question code generates this Log.

231  data convert;
232    set employees;
233    ID=put(ID, 5.);
234  run;

NOTE: Character values have been converted to numeric
      values at the places given by: (Line):(Column).
      233:6
NOTE: There were 3 observations read from the data set WORK.EMPLOYEES.
NOTE: The data set WORK.CONVERT has 3 observations and 1 variables.
NOTE: At least one W.D format was too small for the number to be printed.
      The decimal may be shifted by the "BEST" format.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

the (column) 6 means the position of the Put statement. So even though Put created a character value SAS shows the conversion message that it attempted to assign a character to numeric variable, which it basically doesn't, using internal automatic conversion routines.

I intentionally included a value of ID longer than would be used by a 5. format. See the Note about the W.D too small? That is because there were 7 digits for the last value and 5.0 (the w.d) can't display all of them.

If you look at the resulting data set Convert you will see that the last value of my example Employee set now has a value of 1230000.

That is because in the second part of the note the BEST. format was used instead of the 5 to do something with the value that wouldn't fit.

 

Two things to take away from this besides the fixed variable type:

1) READ THE LOG. Messages about conversions either way generally mean something may be going wrong with the values.

2) Make sure that you control any conversion. The are many places that SAS attempts to help when you may not realize that conversions may be needed. Generally things go okay but there are instances, such as this, where you may get quite different than expected results because of the use of the BEST format (and generally BEST12. by default).

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 304 views
  • 2 likes
  • 3 in conversation