BookmarkSubscribeRSS Feed
srikanthyadav44
Quartz | Level 8

dear all, 

 my dataset has company_name( as character) and 84 other variables. 

out of the 84 variables some variables are in character form and some variables are in numeric form.

 I have to convert all the variables except, company_name into numeric in one attempt

can anybody suggest the SAS code to do this.

thanks in advance  

the format of my dataset is 

company_namevar1var2………………………………var84
Talwalkars Better Value Fitness Ltd50.0642.5  37.9
Tarapur Transformers Ltd52.0643.75  39.65
GB Global Ltd51.0643.5  38.9
Jaypee Infratech Ltd53.0644.75  40.65
SJVN Ltd52.0644.5  39.9
Rainbow Papers Ltd54.0645.75  41.65
Sundaram Multi Pap Ltd53.0645.5  40.9
Pioneer Distilleries Ltd55.0646.75  42.65
Technofab Engineering Ltd54.0646.5  41.9
Denso India Pvt Ltd56.0647.75  43.65
Manappuram Finance Ltd55.0647.5  42.9
Lakshmi Finance and Industrial Corp Ltd57.0648.75  44.65
Hindustan Media Ventures Ltd56.0648.5  43.9
Tilaknagar Industries Ltd58.0649.75  45.65
Indian Metals and Ferro Alloys Ltd57.0649.5  44.9
Bliss GVS Pharma Ltd59.0650.75  46.65
Emami Realty Ltd58.0650.5  45.9
13 REPLIES 13
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
  COMPANY_NAME='A';	VAR1='2';	VAR2='2.2';
run;	

proc contents data=HAVE out=CONT noprint;
run;

proc sql noprint;
  select cat('_',NAME,'=input(',NAME,',best.);drop ',NAME,'; rename _',NAME,'=',NAME) 
     into :code separated by ';'
  from CONT
  where NAME like 'VAR%' and TYPE=1;
quit;       

data WANT;  
  set HAVE;
  &code;
run;

Note that the where clause can be simplified. Keep the selection criteria that you prefer if you want.

 

 

srikanthyadav44
Quartz | Level 8

dear ChrisNZ

thanks for responding to my query

the code suggested by you is not working. 

 

the variables along with their type is as follows 


 Num8COMMA15.2 ESG2000
ESG2001Num8COMMA15.2 ESG2001
ESG2002Num8COMMA15.2 ESG2002
ESG2003Num8COMMA15.2 ESG2003
ESG2004Num8COMMA15.2 ESG2004
ESG2005Num8COMMA15.2 ESG2005
ESG2006Num8COMMA15.2 ESG2006
ESG2007Num8COMMA15.2 ESG2007
ESG2008Num8COMMA15.2 ESG2008
ESG2009Num8COMMA15.2 ESG2009
ESG2010Num8COMMA15.2 ESG2010
ESG2011Num8COMMA15.2 ESG2011
ESG2012Num8COMMA15.2 ESG2012
ESG2013Num8COMMA15.2 ESG2013
ESG2014Num8COMMA15.2 ESG2014
ESG2015Char1$1.$1.ESG2015
ESG2016Char1$1.$1.ESG2016
ESG2017Char1$1.$1.ESG2017
ESG2018Char1$1.$1.ESG2018
ESG2019Char1$1.$1.ESG2019
ESG2020Char1$1.$1.ESG2020
Environment2000Num8COMMA15.2 Environment2000
Environment2001Num8COMMA15.2 Environment2001
Environment2002Num8COMMA15.2 Environment2002
Environment2003Num8COMMA15.2 Environment2003
Environment2004Num8COMMA15.2 Environment2004
Environment2005Num8COMMA15.2 Environment2005
Environment2006Num8COMMA15.2 Environment2006
Environment2007Num8COMMA15.2 Environment2007
Environment2008Num8COMMA15.2 Environment2008
Environment2009Num8COMMA15.2 Environment2009
Environment2010Num8COMMA15.2 Environment2010
Environment2011Num8COMMA15.2 Environment2011
Environment2012Num8COMMA15.2 Environment2012
Environment2013Num8COMMA15.2 Environment2013
Environment2014Num8COMMA15.2 Environment2014
Environment2015Char1$1.$1.Environment2015
Environment2016Char1$1.$1.Environment2016
Environment2017Char1$1.$1.Environment2017
Environment2018Char1$1.$1.Environment2018
Environment2019Char1$1.$1.Environment2019
Environment2020Char1$1.$1.Environment2020
Governance2000Num8COMMA15.2 Governance2000
Governance2001Num8COMMA15.2 Governance2001
Governance2002Num8COMMA15.2 Governance2002
Governance2003Num8COMMA15.2 Governance2003
Governance2004Num8COMMA15.2 Governance2004
Governance2005Num8COMMA15.2 Governance2005
Governance2006Num8COMMA15.2 Governance2006
Governance2007Num8COMMA15.2 Governance2007
Governance2008Num8COMMA15.2 Governance2008
Governance2009Num8COMMA15.2 Governance2009
Governance2010Num8COMMA15.2 Governance2010
Governance2011Num8COMMA15.2 Governance2011
Governance2012Num8COMMA15.2 Governance2012
Governance2013Num8COMMA15.2 Governance2013
Governance2014Num8COMMA15.2 Governance2014
Governance2015Char1$1.$1.Governance2015
Governance2016Char1$1.$1.Governance2016
Governance2017Char1$1.$1.Governance2017
Governance2018Char1$1.$1.Governance2018
Governance2019Char1$1.$1.Governance2019
Governance2020Char1$1.$1.Governance2020
Social2000Num8COMMA15.2 Social2000
Social2001Num8COMMA15.2 Social2001
Social2002Num8COMMA15.2 Social2002
Social2003Num8COMMA15.2 Social2003
Social2004Num8COMMA15.2 Social2004
Social2005Num8COMMA15.2 Social2005
Social2006Num8COMMA15.2 Social2006
Social2007Num8COMMA15.2 Social2007
Social2008Num8COMMA15.2 Social2008
Social2009Num8COMMA15.2 Social2009
Social2010Num8COMMA15.2 Social2010
Social2011Num8COMMA15.2 Social2011
Social2012Num8COMMA15.2 Social2012
Social2013Num8COMMA15.2 Social2013
Social2014Num8COMMA15.2 Social2014
Social2015Char1$1.$1.Social2015
Social2016Char1$1.$1.Social2016
Social2017Char1$1.$1.Social2017
Social2018Char1$1.$1.Social2018
Social2019Char1$1.$1.Social2019
Social2020Char1$1.$1.Social2020
company_nameChar107$107.$107.company_name
 
 
i need all the variables, except company_name in numeric format
 
please suggest me an appropriate SAS code
thanks in advance
 
 
 
ChrisNZ
Tourmaline | Level 20

Have you tried understanding the code to adapt it?

The variables you show now have a different name so you must use these names.

Also, I agree that the process should be fixed so this kind of situation does not happen.

Closer to your new data:

data HAVE;
  COMPANY_NAME='A';	VAR1='2';	VAR2='2.2';
run;	

proc contents data=HAVE out=CONT noprint;
run;

proc sql noprint;
  select cat('_',NAME,'=input(',NAME,',best.);drop ',NAME,'; rename _',NAME,'=',NAME) 
     into :code separated by ';'
  from CONT
  where NAME ne 'COMPANY_NAME' and TYPE=2;
quit;       

data WANT;  
  set HAVE;
  &code;
run;

  

srikanthyadav44
Quartz | Level 8

i have changed the variable names as 

VAR1, VAR2, VAR3..........................VAR84

 still i am not getting the desired output

Rows 1-1
Total rows: 1Total columns: 3
 
COMPANY_NAME
 
VAR1
 
VAR2
 
1A22.2

my output is like above

 

 

Kurt_Bremser
Super User

How does your data arrive in SAS?

- from direct access to a database

- read from a website

- import from files (if that is the case, which file format)

- other (please describe)

srikanthyadav44
Quartz | Level 8

import from excel file 

Kurt_Bremser
Super User

So the usual issue of using the worst file format for data transfer.

The best solution: avoid Excel files, save the data to text files (csv) from Excel and read this with a data step where you keep total control over the data types and can deal with special non-numeric values by using a custom informat.

 

srikanthyadav44
Quartz | Level 8

As you have suggested, i have saved my excel file in CSV format and uploaded it into SAS. 

still, i am getting the same problem 

Alphabetic List of Variables and Attributes# Variable Type Len Format Informat44454647484950515253545556575859606162636465666768697071727374757677787980818283848523242526272829303132333435363738394041424323456789101112131415161718192021221
ESG2000Num8BEST12.BEST32.
ESG2001Num8BEST12.BEST32.
ESG2002Char1$1.$1.
ESG2003Char1$1.$1.
ESG2004Char1$1.$1.
ESG2005Char1$1.$1.
ESG2006Char1$1.$1.
ESG2007Char1$1.$1.
ESG2008Char1$1.$1.
ESG2009Char1$1.$1.
ESG2010Char1$1.$1.
ESG2011Char1$1.$1.
ESG2012Char1$1.$1.
ESG2013Char1$1.$1.
ESG2014Char1$1.$1.
ESG2015Char1$1.$1.
ESG2016Char1$1.$1.
ESG2017Char1$1.$1.
ESG2018Char1$1.$1.
ESG2019Char1$1.$1.
ESG2020Char1$1.$1.
Environment2000Num8BEST12.BEST32.
Environment2001Num8BEST12.BEST32.
Environment2002Char1$1.$1.
Environment2003Char1$1.$1.
Environment2004Char1$1.$1.
Environment2005Char1$1.$1.
Environment2006Char1$1.$1.
Environment2007Char1$1.$1.
Environment2008Char1$1.$1.
Environment2009Char1$1.$1.
Environment2010Char1$1.$1.
Environment2011Char1$1.$1.
Environment2012Char1$1.$1.
Environment2013Char1$1.$1.
Environment2014Char1$1.$1.
Environment2015Char1$1.$1.
Environment2016Char1$1.$1.
Environment2017Char1$1.$1.
Environment2018Char1$1.$1.
Environment2019Char1$1.$1.
Environment2020Char1$1.$1.
Governance2000Num8BEST12.BEST32.
Governance2001Num8BEST12.BEST32.
Governance2002Char1$1.$1.
Governance2003Char1$1.$1.
Governance2004Char1$1.$1.
Governance2005Char1$1.$1.
Governance2006Char1$1.$1.
Governance2007Char1$1.$1.
Governance2008Char1$1.$1.
Governance2009Char1$1.$1.
Governance2010Char1$1.$1.
Governance2011Char1$1.$1.
Governance2012Char1$1.$1.
Governance2013Char1$1.$1.
Governance2014Char1$1.$1.
Governance2015Char1$1.$1.
Governance2016Char1$1.$1.
Governance2017Char1$1.$1.
Governance2018Char1$1.$1.
Governance2019Char1$1.$1.
Governance2020Char1$1.$1.
Social2000Num8BEST12.BEST32.
Social2001Num8BEST12.BEST32.
Social2002Char1$1.$1.
Social2003Char1$1.$1.
Social2004Char1$1.$1.
Social2005Char1$1.$1.
Social2006Char1$1.$1.
Social2007Char1$1.$1.
Social2008Char1$1.$1.
Social2009Char1$1.$1.
Social2010Char1$1.$1.
Social2011Char1$1.$1.
Social2012Char1$1.$1.
Social2013Char1$1.$1.
Social2014Char1$1.$1.
Social2015Char1$1.$1.
Social2016Char1$1.$1.
Social2017Char1$1.$1.
Social2018Char1$1.$1.
Social2019Char1$1.$1.
Social2020Char1$1.$1.
company_nameChar39$39.$39.
 

 

SASKiwi
PROC Star

Please post the complete SAS log of your program to import your CSV. Once you have done that we can explain how to change it to fix your problem.

Kurt_Bremser
Super User

Do NOT use PROC IMPORT to read csv files. Write the data step yourself.

In your case, the INPUT statement will be quite easy:

input
  (esg2000-esg2020 environment2000-environment2020 governance2000-governance2020 social2000-social2020) (.best32.)
  company_name :$39.
;
srikanthyadav44
Quartz | Level 8

hi 

i have removed the numeric variables, now my file as only character variables.

please suggest how to convert all the character variables in the file into numeric in one step.

 

thanks in advance 

ballardw
Super User

Typically if you have variables that should be numeric or not then how you read the data into a SAS data set is the actual issue. You can fix this set but if the process stays the same then the next file needs to be "fixed" and not unlikely with a different set of problem variables.

 

Read the data as needed the first time and you don't have to chase down "fixes". The proper data step to read the file would likely be a best practice "single step".

 

One big issue is how those character values look. If there are things like currency symbols or commas then we need to know by variable so the appropriate informat can be used for the conversion. If you have other non-numeric values such as NULL, NA, MISSING, NOT RECORDED, or just plain random letters, then we need to know what values you expect to appear in the resulting data for each different not-obviously a number text value.

We had someone a couple of weeks ago that expected to turn 860-ABC-44 or similar into numbers without telling us what the numeric value would be or the rules for deriving it.

andreas_lds
Jade | Level 19

By fixing the obviously broken process reading the data.

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
  • 13 replies
  • 2731 views
  • 2 likes
  • 6 in conversation