Hello,
I have a sample data like below:
```
DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;
```
I would like to do following task:
1. build about dataset; e1:ab are num. vars.
2. when ab=1, replace e4 value to "NE".
As e4 currently is num., I think I need to change it to char. and then use if... then to change the value. However when I try that , i got "
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column)."
Could anyone give me an example how to correctly do this?
My current code is:
```
data grades2;
set grades;
e4=put(e4, 8.);
if ab= 1 then e4="NE";
run;
```
@stataq wrote:
Thanks.
One silly question, is it a easy way to build my sample data. I currently only know how to build one looks
like that but all char. 😅
Do you mean how to read in your example datalines?
DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;
In general you should DEFINE the variables using a LENGTH statement (or possibly the ATTRIB statement with the LENGTH= option). So if the first two variables are first name and last name then perhaps you should set them to be character of length 20 each. The other 5 variables look like numbers so just set the length to 8 (SAS stores all numbers as 64-bit floating point values, which require 8 bytes to store.)
length fname $20 lname $20 x1 x2 x3 x4 x5 8 ;
Then when you get to writing the INPUT statement you can just list the variable's names. Or even use a variable list.
data have;
length fname $20 lname $20 x1-x5 8 ;
input fname -- x5;
DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;
Note those particular lines will work fine since every names is exactly two words long. But what if you had
Billy Ray Smith 78 82 86 69 0
instead?
If the values of one of the variables contain spaces you might want to use something other than space as the delimiter between the values. So perhaps the | character. To tell the data step what delimiter it should recognize you need an INFILE statement. You can use one for in-line data, just use DATALINES (or its alias CARDS) as the fileref.
data have;
infile datalines dsd dlm='|' ;
length fname $20 lname $20 x1-x5 8 ;
input fname -- x5;
DATALINES;
Alexander|Smith|78|82|86|69|0
Billy Ray|Smith|78|82|86|69|0
John|Simon|88|72|86|.|1
Patricia|Jones|98|92|92|99|0
Jack|Benedict|54|63|71|49|0
Rene|Porter|100|62|88|.|1
;
You can't change a variable's type. If e4 is numeric due to the SET GRADES statement, it can't be made into a character variable.
However, you could change the name of the incoming e4 variable, which means you can then create a new e4 variable of whatever type you want. Replace your
set grades;
statement with something like
set grades (rename=(e4=_e4));
then calculate the new e4 using the values in variable _e4.
Thanks.
One silly question, is it a easy way to build my sample data. I currently only know how to build one looks like that but all char. 😅
@stataq wrote:
Thanks.
One silly question, is it a easy way to build my sample data. I currently only know how to build one looks
like that but all char. 😅
Do you mean how to read in your example datalines?
DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;
In general you should DEFINE the variables using a LENGTH statement (or possibly the ATTRIB statement with the LENGTH= option). So if the first two variables are first name and last name then perhaps you should set them to be character of length 20 each. The other 5 variables look like numbers so just set the length to 8 (SAS stores all numbers as 64-bit floating point values, which require 8 bytes to store.)
length fname $20 lname $20 x1 x2 x3 x4 x5 8 ;
Then when you get to writing the INPUT statement you can just list the variable's names. Or even use a variable list.
data have;
length fname $20 lname $20 x1-x5 8 ;
input fname -- x5;
DATALINES;
Alexander Smith 78 82 86 69 0
John Simon 88 72 86 . 1
Patricia Jones 98 92 92 99 0
Jack Benedict 54 63 71 49 0
Rene Porter 100 62 88 . 1
;
Note those particular lines will work fine since every names is exactly two words long. But what if you had
Billy Ray Smith 78 82 86 69 0
instead?
If the values of one of the variables contain spaces you might want to use something other than space as the delimiter between the values. So perhaps the | character. To tell the data step what delimiter it should recognize you need an INFILE statement. You can use one for in-line data, just use DATALINES (or its alias CARDS) as the fileref.
data have;
infile datalines dsd dlm='|' ;
length fname $20 lname $20 x1-x5 8 ;
input fname -- x5;
DATALINES;
Alexander|Smith|78|82|86|69|0
Billy Ray|Smith|78|82|86|69|0
John|Simon|88|72|86|.|1
Patricia|Jones|98|92|92|99|0
Jack|Benedict|54|63|71|49|0
Rene|Porter|100|62|88|.|1
;
Create a format which displays missing as NE:
proc format;
value mynum
. = "NE"
other = [2.]
;
run;
proc datasets lib=work;
modify have;
format e4 mynum.;
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.