BookmarkSubscribeRSS Feed
ks94
Obsidian | Level 7

hello , 

Please i need your help for a problem. I want to change the type of variables (numeric to character).

I have a numeric variable that I want to change to a character. Thank you for helping me please.
I'm a beginner in SAS. If you want i can put the type file .

 

  

15 REPLIES 15
PaigeMiller
Diamond | Level 26

You can't change the type of a variable.

 

You can create a new variable that is character from a numeric variable. You can use any format that works. Here is an example using the format 8.

char_var=put(numeric_var,8.);
--
Paige Miller
ks94
Obsidian | Level 7

THANKS YOU BUT can you give me the inverse method ? 

thanks!

PaigeMiller
Diamond | Level 26

@ks94 wrote:

THANKS YOU BUT can you give me the inverse method ?

You asked for creating character from numeric. That's what I gave you. Now you say you want the inverse? Please clarify, which is it?

 
 
--
Paige Miller
ks94
Obsidian | Level 7
change type of variable character to numeric
Tom
Super User Tom
Super User

How did you create variables?  All you showed as a text file.  Everything in a text file is character.  When you read the values you can decide what type of variable you create.

If you read it with PROC IMPORT then that proc will have to guess how to define the variable based on the example values it sees in this one data file.

 

But if you write your own data step to read it you get to define the variables.  It you don't make any attempt to define them then they will all be numbers.  If you define the type and length with a LENGTH statement first then the INPUT statement can be simpler.

data want;
  infile 'c:\downloads\Data_projet_AFRIQUE2.txt' dsd dlm=';' firstobs=2 truncover;
  length
    case $10 cc3 $30 country $8
    year systemic_crisis exch_usd 
    domestic_debt_in_default 
    sovereign_external_debt_default
    gdp_weighted_default
    inflation_annual_cpi
    independence
    currency_crises
    inflation_crises 8
    banking_crisis  $20
  ;
  input case --  banking_crisis ;
  informat _numeric_ commax.;
run;

 

ks94
Obsidian | Level 7

the variables were created with proc import. the variables were created with proc import and I want to change the type of the variables .
e.g.: the variable alphanum , i want to convert to numeric without .

 

Tom
Super User Tom
Super User

The best solution is to NOT create the variables using PROC IMPORT.  PROC IMPORT on text files is by definition a guessing process.  

There is not a variable in that dataset named alphanum, which variable(s) do you want to convert?

Is your problem with the values with the commas in them?

1;DZA;;;0;0,05168;0;0;0;11,20389701;0;0;0;no_crisis

The way the commas are placed and the fact that semi-colon is used as the delimiter instead of comma makes it looks like the commas should treated as decimal places.  So you probably need to use the COMMAX informat to convert the strings to numbers.  But your locale setting might impact that.

data want ;
  set have ;
  newvar = input(oldvar,commax32.);
run;

If you want the values to print using comma instead of period to mark the decimal place you might need to attach the COMMAX format to the value.  But note that once you have the value as a number the format attached does not change the value stored, just how it is displayed as text.  The number one thousand two hundred thirty four and a half is the stored the same whether it is displayed as 1,234.5 or 1.234,5 or 001234.500.

 

Tom
Super User Tom
Super User

Try just changing your LOCALE setting:

https://documentation.sas.com/?docsetId=proc&docsetTarget=p12uk352fte2h1n1efh4r44dmxmp.htm&docsetVer...

 

Beginning with SAS 9.4M3, PROC IMPORT uses the NLNUM informat instead of the COMMA informat. When you import a file that contains values such as 14,000.01 that have commas, the COMMA informat removes the commas and other non-numeric characters from the numerical values. Removing these characters can cause interpretation errors in the values. NLNUM prevents these errors by using the specified value of the LOCALE system option to interpret numerical values that have commas.

 

For example, to enter the numerical equivalent of fourteen thousand and one hundredth, a person specifying LOCALE=English_UnitedStates would enter 14,000.01. A person specifying LOCALE=French_France would enter 14.000,01. NLNUM interprets either input value correctly and writes the correct value based on the specified locale. If you read in 14.000,01 with NLNUM and LOCALE=French_France, store it in a data set, and then write it with NLNUM and LOCALE=English_UnitedStates, it is displayed as 14,000.01.

Kurt_Bremser
Super User

@ks94 wrote:

the variables were created with proc import. the variables were created with proc import and I want to change the type of the variables .
e.g.: the variable alphanum , i want to convert to numeric without .

 


Do NOT (as in NEVER) use proc import if you want control over what happens. Use a data step to read the file.

You can use proc import once to create the data step which you then adapt to your needs.

Patrick
Opal | Level 21

@ks94 

Proc Import lets you quickly read an external file into a SAS table. Using Proc Import means that you delegate the analysis of the source data to SAS - and this also often works as desired. If it doesn't work then you need to take control back meaning you need to write your own datastep.

Proc Import also creates a SAS datastep to read a text file. This datastep gets written to the SAS log. So if you use Proc Import as discussed here and execute it then you'll see in the SAS log the data step. What you can do now is to copy/paste this part of the SAS log into a programming window. You then can use the ALT key and select all the columns on the left side which are not code but part of the log, and then delete them. Also delete any log page header lines (if any). Now you've got fully valid SAS datastep code reading your external file.

 

Looking at the result of the Proc Import I guess you've got an issue with column exch_usd. It appears Proc Import is struggling with the European style comma as decimal separator - at least when executing with a non-European local. I've applied the required change already to below code. Compare below with what you get originally (in your SAS log executing Proc Import) and you should have all the guidance on how to proceed.

 

The only thing you need to change in below code for it to execute in your environment is infile '<path>/Data_projet_AFRIQUE1.txt'  to what you get in your SAS log using Proc Import.

 

data PREP.AFRICA;
  %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
  infile '<path>/Data_projet_AFRIQUE1.txt' delimiter = ';' MISSOVER DSD lrecl=32767 firstobs=2;
  informat case best32.;
  informat cc3 $3.;
  informat country $24.;
  informat year best32.;
  informat systemic_crisis best32.;
  informat exch_usd commax.;
  informat domestic_debt_in_default best32.;
  informat sovereign_external_debt_default best32.;
  informat gdp_weighted_default nlnum32.;
  informat inflation_annual_cpi nlnum32.;
  informat independence best32.;
  informat currency_crises best32.;
  informat inflation_crises best32.;
  informat banking_crisis $9.;
  format case best12.;
  format cc3 $3.;
  format country $24.;
  format year best12.;
  format systemic_crisis best12.;
  format exch_usd 32.10;
  format domestic_debt_in_default best12.;
  format sovereign_external_debt_default best12.;
  format gdp_weighted_default nlnum12.;
  format inflation_annual_cpi nlnum12.;
  format independence best12.;
  format currency_crises best12.;
  format inflation_crises best12.;
  format banking_crisis $9.;
  input
    case
    cc3  $
    country  $
    year
    systemic_crisis
    exch_usd  
    domestic_debt_in_default
    sovereign_external_debt_default
    gdp_weighted_default
    inflation_annual_cpi
    independence
    currency_crises
    inflation_crises
    banking_crisis  $
  ;
  if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
run;

 

 

 

ks94
Obsidian | Level 7
hi ,
I'm beginner , so i have a question why you put <> after
<> ? can you explain " if _ERROR_ then call
symputx('_EFIERR_',1);" .?
thanks you.
Patrick
Opal | Level 21

"why you put <>"  I guess you're talking about this bit: infile '<path>/Data_projet_AFRIQUE1.txt'

The bit in the <> is a placeholder and what you need to replace with the actual path in your environment (which the Proc Import generated datastep code in the SAS log will provide).

Writing it this way is inspired by SAS syntax conventions in the documentation (also there the brackets have a slightly different meaning).

 

if _ERROR_ then call symputx('_EFIERR_',1);

That's what Proc Import generates. As a beginner you haven't got to SAS macro variables yet - so just ignore this bit for now. 

This code is normally not something people write for a SAS datastep - but because Proc Import generates such code I didn't want to remove it - but don't hesitate to delete below two lines from your code.

....
%let _EFIERR_ = 0; 
....
if _ERROR_ then call symputx('_EFIERR_',1);  
.... 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 15 replies
  • 2856 views
  • 2 likes
  • 5 in conversation