Hello everyone,
I have several data sets that all contain the same variables. Problem is, one date variable is sometimes stored as a numeric variable, sometimes as a character variable.
I tried the following :
if vtype(Latitude) = "N" then Latitude = Latitude;
else Latitude = put(Latitude, 8.);
However I am getting an error:
Variable Latitude has been defined as both character and numeric.
Any suggestion?
Thanks,
Nick
You can't change the type of an existing variable in SAS, you can only assign the type of new variables.
What type do you want Latitude to be in all of your datasets? It would help if you posted a sample of your data.
Latitude needs to be numeric, so I thought of converting it like that.
I tried writing it as:
if vtype(Latitude) = "N" then Latitude1= Latitude;
else Latitude1 = put(Latitude, 8.);
and dropping, Latitude and renaming Latitiude1 to Latitude, but I get the same error.
Use the CATS() function to convert the current variable into a string. Then use the INPUT() function to convert that to a number.
data have ;
latitude='56.3';
run;
data want;
set have;
number=input(cats(latitude),32.);
drop latitude;
rename number=latitude;
run;
data have ;
latitude=56.3;
run;
data want;
set have;
number=input(cats(latitude),32.);
drop latitude;
rename number=latitude;
run;
Unfortunately it seems you need to know the variable type ahead of time to get this to work without warnings/notes. If warning/notes are fine, it can be done in a single step the would works regardless of input type.
However, the best solution is to have it read in properly in the first place. Did you import text files using PROC IMPORT and are now trying to merge them? Are all the input files formatted exactly the same? If yes, there is a faster way to fix this issue. If not, there are other ways, just a little bit more work.
@Nikolasas wrote:
Hello everyone,
I have several data sets that all contain the same variables. Problem is, one date variable is sometimes stored as a numeric variable, sometimes as a character variable.
I tried the following :
if vtype(Latitude) = "N" then Latitude = Latitude;
else Latitude = put(Latitude, 8.);
However I am getting an error:
Variable Latitude has been defined as both character and numeric.
Any suggestion?
Thanks,
Nick
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: