BookmarkSubscribeRSS Feed
Nikolasas
Calcite | Level 5

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

5 REPLIES 5
SASKiwi
PROC Star

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.

Nikolasas
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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;
Astounding
PROC Star
The PUT function generates a character string, not a numeric. Use the INPUT function instead.
Reeza
Super User

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


 

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3463 views
  • 3 likes
  • 5 in conversation