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

Hello,

 

I have a sample data like below:

stataq_0-1693361831916.png

 

```

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;

```

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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
;

 

 

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
stataq
Quartz | Level 8

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. 😅

Tom
Super User Tom
Super User

@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
;

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 561 views
  • 2 likes
  • 4 in conversation