Converting $1. to best12

Reply
Occasional Contributor
Posts: 9

Converting $1. to best12

Hello!

 

I have been trying to convert some variables from the informat $1. to BEST12. but it's not working. Help please!

 

data analysis.myco;
set work.new;


informat afb1 afm1 afg1 afg2 ata cit dhcit don donglca zen zenglca
afb_creat afm_creat afg_creat ota_creat dh_cit_creat don_creat donglca_creat zen_creat zenglca_creat $1.;


format afb1 afm1 afg1 afg2 ata cit dhcit don donglca zen zenglca
afb_creat afm_creat afg_creat ota_creat dh_cit_creat don_creat donglca_creat zen_creat zenglca_creat best12.;


input afb1 afm1 afg1 afg2 ata cit dhcit don donglca zen zenglca
afb_creat afm_creat afg_creat ota_creat dh_cit_creat don_creat donglca_creat zen_creat zenglca_creat;


run;

Super User
Posts: 10,500

Re: Converting $1. to best12

Once a SAS variable has been created you cannot change it's type. You are attempting to change a character to numeric.

Are you actually INPUTing data (reading from an external file)? If so then change the informat to best12. to read the data.

 

If you want convert you have to decide if you want to keep the variable names (which will require a bunch of rename code) or are willing to accept new variable names.

Creating new numeric variables (one way, there are others), using just part of the variables.

data analysis.myco;
   set work.new;
   array h  afb1 afm1 afg1 afg2 ata cit dhcit don donglca zen zenglca ;
   array w  afb1_num afm1_num afg1_num afg2_num ata_num cit_num dhcit_num don_num donglca_num zen_num zenglca_num ;
   do i= 1 to dim(h);
      w[i] = input(h[i],best12.);
   end;
run;

reusing the same variable names

 

data analysis.myco;
   set work.new (rename = (afb1=afb1_old afm1=afm1_old afg1=afg1_old afg2=afg2_old 
         ata=ata_old cit=cit_old dhcit=dhcit_old don=don_old donglca=donglca_old zen=zen_old zenglca=zenglca_old ) 
   ;
   array h  afb1_old afm1_old afg1_old afg2_old ata_old cit_old dhcit_old don_old donglca_old zen_old zenglca_old;
   array w  afb1 afm1 afg1 afg2 ata cit dhcit don donglca zen zenglca ;
   do i= 1 to dim(h);
      w[i] = input(h[i],best12.);
   end;   
   drop i afb1_old afm1_old afg1_old afg2_old ata_old cit_old dhcit_old don_old donglca_old zen_old zenglca_old;
run;

 

Better would be to go back to what ever created work.new and read the data values correctly the first time. That many variables with an existing format of $1. tells me the data likely came from proc import AND that the values may be blank.

Occasional Contributor
Posts: 9

Re: Converting $1. to best12

Hi!

Thanks for the response.

Yes, I used proc import. The data is from .csv file. I made sure to save
all the variables in numeric format, but in sas it's not showing as
numeric. So, even when I do go back to the original .csv file, it does not
work.


Super User
Posts: 5,083

Re: Converting $1. to best12

If SAS made the variables character when you imported, there is probably a reason for it.  If the output doesn't get too long, you can try:

 

proc freq data=new;

tables _character_;

run;

 

You will likely see values for these variables that cannot be stored as numeric.  You would have to decide what to do about them, before anybody can write a program to fix the situation.

 

Also note, if you are trying some of the other suggestions posted here, there is no such thing as a best12. informat.  You could simply use the 12. informat instead.  Best12. is a format, but not an informat.

New Contributor
Posts: 4

Re: Converting $1. to best12

Did you try input function ?

 

a = input(variablename, best12.); 

Super User
Super User
Posts: 6,500

Re: Converting $1. to best12

There is no need to use PROC IMPORT to read a CSV file, especially one that you already know what the variables are.

data want ;
   infile 'myfile.csv' dsd truncover firstobs=2;
   input afb1 afm1 afg1 afg2 ata cit dhcit 
        don donglca zen zenglca afb_creat
        afm_creat afg_creat ota_creat dh_cit_creat
        don_creat donglca_creat zen_creat zenglca_creat
  ;
run;

There is no need to attach either informats or formats to most variables.

Ask a Question
Discussion stats
  • 5 replies
  • 97 views
  • 1 like
  • 5 in conversation