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 .
In the data step with which you read the file, use a character informat for reading that column.
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;
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?
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.
Try just changing your LOCALE setting:
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.
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.
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;
"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); ....
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.