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

I believe I am taking the right steps to convert the following character to a numeric one, however, when I run the code, the results in the graph show as "."

 

*2. In a dataset called ATL2 made from ATL1, use an INPUT statement to convert the character
variable AttendX to a numeric variable called AttendXNum.;
data ATL2;
set ATL1;
AttendX= 'AttendXNum';
AttendXNum= Input (AttendX, AttendXNum.);
run;

drop AttendX;
rename AttendXNum = AttendX;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@saza wrote:

Your solution worked! However, when I try to convert a numeric variable to a character using "put" statement, it doesn't work. I'm certain there is a different way to go about this.

The question is: In a dataset called ATL3 made from ATL2, use a PUT statement to convert the numeric
variable Grade to a character variable called GradeChar.

My code:

data ATL3 (rename=(Grade=GradeChar));
set ATL2;
GradeChar= put (Grade,GradeChar);
drop Grade;
run;

The second argument in PUT statement is a FORMAT name. Format names either end in a . or . and number indicating the number of decimal places involved. Likely, if you have a format named Gradchar, your statement should look like

Gradechar= put(grade, gradechar.);

Or possibly if you do not actually have a format defined in your current session

Gradechar= put(grade, 2.);

Format and Informat names will always have dot in them somewhere.

Your original code would create an error similar to the one shown below.

613  data junk;
614     set sashelp.class;
615     agechar= put(age,agechar);
                         -------
                         85
                         76
ERROR 85-322: Expecting a format name.

ERROR 76-322: Syntax error, statement will be ignored.

616  run;

The dot in the name lets SAS know you intend a format.

There are other versions of the Put function, PUTN and PUTC that will allow you to use a variable to hold text that contains the format, the N is for numeric and C for character formats. (and corresponding INPUTN and INPUTC to read values).

A simple example:

data junk;
   set sashelp.class;
   fmt='best2.';
   agechar= putn(age,fmt);
run;

This might be useful if you need to change appearance of the result based on another variable such as displaying currency values with different symbols such as dollars or Euros (not conversion of values just different symbols than $ )

 

View solution in original post

10 REPLIES 10
StatDave
SAS Super FREQ

One easy way to convert character to numeric (assuming the values are numbers but in a character type variable) is to use a math operator. For example:  numvar=charvar+0;

A more general solution that can do many conversions very flexibly and add labels is the CtoN macro

PaigeMiller
Diamond | Level 26

You need to use some numeric informat, which could be something as simple as 8. but there might be other informats that work better, depending on what the values of AttendX are.

 

data ATL2;
    set ATL1;
    AttendXNum= Input (AttendX, 8.);
    drop AttendX;
run;

 

Also, I don't think you can rename the variable AttendXNum to AttendX in this data step. Next time you use AttendXNum you can rename it if you want. Example:

 

proc print data=atl2(rename=(attendxnum=attendx));
run;
--
Paige Miller
saza
Quartz | Level 8
It's basically the attendance variable in the grades. So the values in the columns are either 1 or 0
ballardw
Super User

If the variable is as in the attached data set the code would look more like:

data ATL2;
set ATL1;
AttendXNum= Input (AttendX,1.);
run;

Where 1. is the informat to read a single digit number. Any value of Attendx that doesn't have a digit as the first character would result in missing.

 

Your code showing the below would require you to create a custom informat name AttendXNum using Proc Format with an INVALUE statement. Otherwise there is no informat of that name. If you attempted such then you should share the Proc Format code.

AttendXNum= Input (AttendX, AttendXNum.);

 

Your rename and drop need some help though. If you have a drop and rename in the body of the data step you may not like the result. The rename could be done as a data set option on the data statement:

 

data ATL2 (rename=(AttendX=AttendXNum));
set ATL1;
AttendXNum= Input (AttendX,1.);
drop Attendx;
run;

Or rename the character version when read, use that name and drop for the conversion

 

data ATL2 ;
set ATL1 (rename=(AttendX=AttendXChar));
AttendX= Input (AttendXChar,1.);
drop AttendxChar;
run;

 

saza
Quartz | Level 8

Your solution worked! However, when I try to convert a numeric variable to a character using "put" statement, it doesn't work. I'm certain there is a different way to go about this.

The question is: In a dataset called ATL3 made from ATL2, use a PUT statement to convert the numeric
variable Grade to a character variable called GradeChar.

My code:

data ATL3 (rename=(Grade=GradeChar));
set ATL2;
GradeChar= put (Grade,GradeChar);
drop Grade;
run;
ballardw
Super User

@saza wrote:

Your solution worked! However, when I try to convert a numeric variable to a character using "put" statement, it doesn't work. I'm certain there is a different way to go about this.

The question is: In a dataset called ATL3 made from ATL2, use a PUT statement to convert the numeric
variable Grade to a character variable called GradeChar.

My code:

data ATL3 (rename=(Grade=GradeChar));
set ATL2;
GradeChar= put (Grade,GradeChar);
drop Grade;
run;

The second argument in PUT statement is a FORMAT name. Format names either end in a . or . and number indicating the number of decimal places involved. Likely, if you have a format named Gradchar, your statement should look like

Gradechar= put(grade, gradechar.);

Or possibly if you do not actually have a format defined in your current session

Gradechar= put(grade, 2.);

Format and Informat names will always have dot in them somewhere.

Your original code would create an error similar to the one shown below.

613  data junk;
614     set sashelp.class;
615     agechar= put(age,agechar);
                         -------
                         85
                         76
ERROR 85-322: Expecting a format name.

ERROR 76-322: Syntax error, statement will be ignored.

616  run;

The dot in the name lets SAS know you intend a format.

There are other versions of the Put function, PUTN and PUTC that will allow you to use a variable to hold text that contains the format, the N is for numeric and C for character formats. (and corresponding INPUTN and INPUTC to read values).

A simple example:

data junk;
   set sashelp.class;
   fmt='best2.';
   agechar= putn(age,fmt);
run;

This might be useful if you need to change appearance of the result based on another variable such as displaying currency values with different symbols such as dollars or Euros (not conversion of values just different symbols than $ )

 

saza
Quartz | Level 8
Aside from the answer, I appreciated the explanation because it helped me solve the next two questions! Only one left to go!
Tom
Super User Tom
Super User

The original problem said nothing about renaming or dropping anything.

The INPUT() function does not care if the informat width is larger than the length of the strings being converted. So it is easiest to just use 32. as the informat as 32 to is the maximum width the input supports.

data ATL2 ; 
  set ATL1;
  AttendXNum= input(AttendX,32.);
run;

 

If you did want the new variable to have the same name as the original then just include a RENAME statement. 

You will either need to drop the old variable:

data ATL2 ; 
  set ATL1;
  AttendXNum=input(AttendX,32.);
  rename attendxnum=AttendX;
  drop attendx;
run;

Or rename it also.

data ATL2 ; 
  set ATL1;
  AttendXNum=input(AttendX,32.);
  rename attendxnum=AttendX attendx=AttendXchar;
run;

No need to confuse yourself by trying to learn about dataset options at this point.

 

saza
Quartz | Level 8
Hi tom,

I read your reply earlier and was hoping you could help me configure the code for my final question using this dataset:
The question:
In a dataset called ATL4 made from ATL3, use IF-ELSE statements to create a variable called
ColorAtd with the following values:
ColorAtd = BR0 when
Group = “Blue” OR “Red” and AttendXNum = 0
ColorAtd = BR1 when
Group = “Blue” OR “Red” and AttendXNum = 1
ColorAtd = OY0 when
Group = “Orange” OR “Yellow” and AttendXNum = 0
ColorAtd = OY1 when
Group = “Orange” OR “Yellow” and AttendXNum = 1;

My Code:
data ATL4;
set ATL3;
If ColorAtd= BRO when Group= "Blue" or "Red" and AttendXNum=0;
else If ColorAtd = BR1 when Group = “Blue” OR “Red” and AttendXNum = 1;
else if ColorAtd = OY0 when Group = “Orange” OR “Yellow” and AttendXNum = 0;
else ColorAtd = OY1 when Group = “Orange” OR “Yellow” and AttendXNum = 1;
run;
The graph comes out but there is no results.

For reference i'm gonna attach the code that led me to this point.
data ATL1;
set Sarah.atlgrades;
if grade >= 85 then GRADEREV = "Excellent";
else if 70<=grade<=84 then graderev="Good";
else if 60<=grade<=69 then GRADEREV = "Okay";
else GRADEREV = "Not so good";
run;
data ATL2 (rename=(AttendX=AttendXNum));
set ATL1;
AttendXNum= Input (AttendX,1.);
drop Attendx;
run;
data ATL3 (rename=(Grade=GradeChar));
set ATL2;
GradeChar= put (Grade,GradeChar.);
drop Grade;
run;

PaigeMiller
Diamond | Level 26

The graph comes out but there is no results.

I cannot understand what this means. What graph are you talking about? What does "no results" mean?

 

 

data ATL4;
set ATL3;
If ColorAtd= BRO when Group= "Blue" or "Red" and AttendXNum=0;
else If ColorAtd = BR1 when Group = “Blue” OR “Red” and AttendXNum = 1;
else if ColorAtd = OY0 when Group = “Orange” OR “Yellow” and AttendXNum = 0;
else ColorAtd = OY1 when Group = “Orange” OR “Yellow” and AttendXNum = 1;
run;

will not work because unless you have a variable named BR0 and BR1 and so on, the code fails. And you can't test two colors that way. And I have no idea what these statements are trying to do, they don't make logical sense. IF statements usually require a THEN, just like in simple speaking language (if it is pouring rain, then we won't go on the hike).

 

Is this what you want?

 

If Group in ("Blue" , "Red") and AttendXNum=0 then ColorAtd= 'BR0';

Please also note that you have typed BR0 in one place (that's a zero on the end) which is not the same as the BRO (with the letter o on the end), you need to pay attention to details like that.


But let's also take a look at the BIG picture. Many beginners code as if they are allowed to perform only one operation in a data step. I don't know why people think this, but it is not true. So you can do almost everything in one data step.

 

data ATL1;
    set Sarah.atlgrades;
    length graderev $ 11;
    if grade >= 85 then GRADEREV = "Excellent";
    else if 70<=grade<=84 then graderev="Good";
    else if 60<=grade<=69 then GRADEREV = "Okay";
    else GRADEREV = "Not so good";
    AttendXNum= Input (AttendX,1.);
    GradeChar= put (Grade,GradeChar.); /* Note: this step won't work */
    If Group in ("Blue" , "Red") and AttendXNum=0 then ColorAtd= 'BR0';
    else if group in ("Blue","Red") and attendxnum=1 then coloratd='BR1';
    else if group in ("Orange","Yellow") and attendxnum=0 then coloratd='OY0';
    else if group in ("Orange","Yellow") and attendxnum=1 then coloratd='OY1';
    drop Attendx grade;
run;
proc datasets library=work nolist;
    modify atl1;
    rename attendxnum = attendx;
run; quit;

The GRADECHAR = command won't work, and I don't know what you are trying to do. Unless there is a custom format named GRADECHAR then you are not using the PUT command properly, as the second argument of the PUT command must be a format.

 

If all you want to do in a step is rename variable(s), then PROC DATASETS is a more efficient way to do this.

 

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 3305 views
  • 1 like
  • 5 in conversation