Help using Base SAS procedures

changing character to numeric

Reply
N/A
Posts: 0

changing character to numeric

I am trying to append two datasets (one and two) that have 6 columns:- Class,Route,Date,Time,Name,Place and Arrival


In file one, Time,Arrival and Place are character and these variables are Numeric in the data set two.

In the dataset two the informat and the format for these numeric variables is:Time8. (for Arrival),Date9. (for Date) and Time8. (for Time). The other three variables viz. Route,Class and Name are character in both the datasets.

I use the code:-


data combine;
merge one two;
by Name;
run;

and get the error that Time,arrival and Place are defined as both character and numeric.


Kindly Suggest.
Sid
New Contributor
Posts: 2

Re: changing character to numeric

Nice that you gave more information about your problem. In merge you should state all "by" columns, like:

data combine;
merge one two;
by Name Date Time Class Route Place Arrival;
run;

For time it's simple, if you have time in HH:MM format use input function to create numeric version of TIME variable:
timenum=input(timechar,time5.);
For Arrival/Place you have to be more specific - if both are numbers, but one stored as a character, use the same input function but with different informat (like best.). If not, and there is relation like 1-Orlando etc., then it's a bigger issue.
N/A
Posts: 0

Re: changing character to numeric

Hi,

When I use the code:-

data char(drop=time_num place_num arrival_num) / view=char;
set week2 (rename=(time=time_num place=place_num arrival=arrival_num));
time_num=INPUT(time,time9.);
place_num=INPUT(place,time8.);
arrival_num= INPUT(arrival,time8.);
run;

Actually, what I want is to convert the three variables Time, Place and Arrival in week 2 into numeric (as other variables are character type in both data sets) so as to merge the data set week 2 with the dataset week 1 (week 1 has these variables as numeric). After using the above code , I use the code:-

data sdsd;
MERGE char week1;
by TIME PLACE ARRIVAL CLASS ROUTE DATE NAME;
run;

The code runs successfully without any error. But there are blank spaces in the merged dataset 'sdsd' with blanks occuring for the dataset 'char' in the three converted variables.

Kindly suggest.

Kritanjli.
Frequent Contributor
Posts: 82

Re: changing character to numeric

Hi,

Maybe you can give an example how your do those character values for these variables look like, especially to those places where blanks occur?

Ieva
N/A
Posts: 0

Re: changing character to numeric

data two has the format:-

Alphabetic List of Variables and Attributes
# Variable Type Len
6 ARRIVAL Char 8
4 TIME Char 8
5 PLACE Char 5
3 CLASS Char 5
1 NAME Char 10
2 CITY Char 4

data one:-

Alphabetic List of Variables and Attributes
# Variable Type Len Format Informat Label
6 ARRIVAL Num 8 TIME8. ARRIVAL8. ARRIVAL
1 TIME Num 8 DATE9. TIME9. DATE
4 PLACE Num 8 TIME8. PLACE8. DEPART
5 CLASS Char 4 $4. $4. CLASS
3 NAME Char 5 $5. $5. NAME
2 CITY Char 5 $5. $5. CITY


I want to merge data one and data two. Kindly suggest.

Kritanjli.
Frequent Contributor
Posts: 106

Re: changing character to numeric

No errors, but blanks in the values? Sounds like you are talking about the formatted values. Otherwise you should have gotten some kind of note/warning messages.

Let me go out on a limb here: is it the missing leading zero in a time value that bothers you? You want 09:10:15 instead of 9:10:15? If so then create a format somewhat like
[pre]
proc format;
picture ztime low-high = '%0H:%0M:%0S' (datatype=time) ;
run ;
[/pre]
Assign this format to your time values
[pre]
format time place arrival ztime. ;
[/pre]
If this is not the problem you are facing then please explain in more detail what "there are blank spaces in .... for the dataset 'char' ...." means exactly.
N/A
Posts: 0

Re: changing character to numeric

I would write the character of the two datasets and the code I used and the Log that I get as -

data week 2 has the format:-

Alphabetic List of Variables and Attributes
# Variable Type Len
6 ARRIVAL Char 8
4 DATE Char 8
5 DEPART Char 5
3 CLASS Char 5
1 NAME Char 10
2 CITY Char 4

data week 1:-

Alphabetic List of Variables and Attributes
# Variable Type Len Format/Informat
6 ARRIVAL Num 8 TIME8.
1 DATE Num 8 TIME9.
4 DEPART Num 8 TIME8.
5 CLASS Char 4 $4.
3 NAME Char 5 $5.
2 CITY Char 5 $5.

I use the code:-

data char(drop=date_num depart_num arrival_num) / view=char;
set week 2 (rename=(date=date_num depart=depart_num arrival=arrival_num));
date_num=INPUT(date,date9.);
depart_num=INPUT(depart,time8.);
arrival_num= INPUT(arrival,time8.);
run;

LOG:-

Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).
311:18 311:24 312:20 312:26 313:22 313:28

Then I use code:-
data sdsd;
MERGE char week1;
by ORIGIN DRIVER DESTINATION DATE DEPART ARRIVAL;
run;

LOG:-

NOTE: There were 24 observations read from the data set WORK.WEEK2.
NOTE: There were 24 observations read from the data set WORK.CHAR.
NOTE: There were 24 observations read from the data set WORK.WEEK1.
NOTE: The data set WORK.SDSD has 48 observations and 6 variables.

Next, I use the command:-

PROC PRINT DATA=sdsd;
run;

But, the output does not give me the entries for the new data "char" for the three variables that I converted from character to numeric.

Kindly suggest, where I'm making a mistake.

regards,
Krit
Frequent Contributor
Posts: 106

Re: changing character to numeric

You have the variable names wrong in your view.
On the SET statement you do the rename with the _num suffix. Do that with a _char suffix (in order to keep the suffix meaningful).
On your var=INPUT(...) statements you should have the target names (i.e. those that you want to keep) on the left side.
Things like:
[pre]
date = input(date_char ...) ;
depart = input(depart_char ...) ;
arrival = input(arrival_char ... );
[/pre]
and then drop the _char suffix variables.
Respected Advisor
Posts: 3,890

Re: changing character to numeric

Hi Sid

To post references for posting the same question in different forums would be kind of courtesy to the ones giving you their time to answer.

So: Same question already posted here by 2 different people. It seems to be a study excercise:
http://groups.google.com.au/group/comp.soft-sys.sas/browse_thread/thread/7ee599125d81eb80?hl=en#

http://groups.google.com.au/group/comp.soft-sys.sas/browse_thread/thread/df7183cf64b52297/f31e01b38f...

Thanks
Patrick
Occasional Contributor
Posts: 8

Re: changing character to numeric

try these codes


data Week2_1 (drop = DATE ARRIVAL DEPART);
set Week2;
DATE_1 = input(DATE, ANYDTDTE10.);
format DATE_1 date9.;
ARRIVAL_1 = input(ARRIVAL, anydttme8.);
format ARRIVAL_1 time8.;
DEPART_1 = input(DEPART, anydttme8.);
format DEPART_1 time8.;
run;


data Week2_2;
set Week2_1;
rename
DATE_1 = DATE
ARRIVAL_1 = ARRIVAL
DEPART_1 = DEPART;
run;

Data new;
set Week1 Week2_2; /* u r trying to append 2 files not merge right?*/
run;

so now data set new should have all the observations
Ask a Question
Discussion stats
  • 9 replies
  • 198 views
  • 0 likes
  • 6 in conversation