BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Biniie
Fluorite | Level 6

 I have a +70 variables in my dataset which I need to change formats on from character variables to numeric. All of the variables names is similar sn_1, sn_2, .... 

 

I plan to use the follow procedure to change the formats: 

data work.xx

sn_1n=input(sn_1, best8.);

run;

 

Is there anyway that I can change the formats of all my variables in single procedure or do I need to do it "manually" for every single one of my variables? 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Biniie wrote:

Thank you very much for your respons. I tried it out, and it would have worked perfectly, but there is a twist. 

 

5 out of the 70 variabels is actually numeric variabels. I can't explain why some of my variables is numeric and why some are defined as character variables. So when I try the procedure you surgested, I get the error "All variables in array list must be the same type, i.e., all numeric or character" which makes perfectly sense. 

 

Any surgestion how to deal with this?




If the variables are expected to be character but are numeric I would guess that someone is using Proc Import to bring data into SAS and not understanding the "guessing based on values" nature of Proc Import, and by default only a few rows of data are examined to guess the variable type, informat and length.

OR a program reading data has the wrong read instructions. It may be that if you want numeric values you should go back to the steps where the data is read and address the issue earlier in the process.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

I would recommend keeping the numeric suffix, and changing the prefix:

 

nsn_1 = input(sn_1, 8.);

 

Note that there is no such thing as a "best8" informat.  SAS will automatically use 8. instead.

 

The right tool to handle many variables int he same fashion:  arrays.  For example:

 

data want;
   set have;
   array sn_ {70};
   array nsn_ {70};
   do k=1 to 70;
      nsn_{k} = input(sn_{k}, 8.);
   end;
   drop k sn_1-sn_70 ;
run;

Dropping the original variables is optional.

Biniie
Fluorite | Level 6

Thank you very much for your respons. I tried it out, and it would have worked perfectly, but there is a twist. 

 

5 out of the 70 variabels is actually numeric variabels. I can't explain why some of my variables is numeric and why some are defined as character variables. So when I try the procedure you surgested, I get the error "All variables in array list must be the same type, i.e., all numeric or character" which makes perfectly sense. 

 

Any surgestion how to deal with this?



Astounding
PROC Star

It can be done, as shown below.  I'm not sure how easy it will be to understand the code.  So for better or worse:

 

options mprint;

data _null_;
   set have (obs=1);
   array sn_ {*} sn_1 - character - sn_70;
   length varlist_sn varlist_nsn $ 2000;
   do k=1 to dim(sn_);
      varlist_sn = catx(' ', varlist_sn, vname(sn_{k} );
      varlist_nsn = catx(' ', varlist_nsn, 'n' || vname(sn_{k});
   end;
   call symputx('sn_vars', varlist_sn);
   call symputx('nsn_vars', varlist_nsn);
run;

data want;
   set have;
   array sn_ {*} &sn_vars;
   array nsn_ {*} &nsn_vars;
   do k=1 to dim(sn_) ;
      nsn_{k} = input(sn_{k}, 8.);
   end;
   drop k &sn_vars;
run;

There are other ways to approach the initial step, but in my mind they are more complex rather than less complex.

 

You are left with a couple of tasks.  Copy the good ones (the original numerics) to new variable names if desired.

 

Test the code ... you have the data, so this is untested code.  It might need a small tweak here or there.  I made a few minors changes, so be sure you are working with the current version of the code.

ballardw
Super User

@Biniie wrote:

Thank you very much for your respons. I tried it out, and it would have worked perfectly, but there is a twist. 

 

5 out of the 70 variabels is actually numeric variabels. I can't explain why some of my variables is numeric and why some are defined as character variables. So when I try the procedure you surgested, I get the error "All variables in array list must be the same type, i.e., all numeric or character" which makes perfectly sense. 

 

Any surgestion how to deal with this?




If the variables are expected to be character but are numeric I would guess that someone is using Proc Import to bring data into SAS and not understanding the "guessing based on values" nature of Proc Import, and by default only a few rows of data are examined to guess the variable type, informat and length.

OR a program reading data has the wrong read instructions. It may be that if you want numeric values you should go back to the steps where the data is read and address the issue earlier in the process.

Ksharp
Super User
data have;
 sn_1='1'; sn_2='2';
run;


data key;
input name $;
cards;
sn_1
sn_2
;

data _null_;
 set key end=last;
 if _n_=1 then call execute('data want;set have;');
 call execute(cats(name,'n=input(',name,',best8.);'));
 if last then call execute('run;');
run;

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

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
  • 5 replies
  • 620 views
  • 6 likes
  • 4 in conversation