BookmarkSubscribeRSS Feed
NadiaK
Fluorite | Level 6

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;

5 REPLIES 5
ballardw
Super User

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.

NadiaK
Fluorite | Level 6
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.


Astounding
PROC Star

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.

OnSAS
Fluorite | Level 6

Did you try input function ?

 

a = input(variablename, best12.); 

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 5 replies
  • 8953 views
  • 1 like
  • 5 in conversation