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

Hello,

I'm trying to convert my character variables which are more than 4000 to numeric and I'd like to also get their labels assigned to new numeric variables.

I found attached PDF document for this purpose, it's working for a few variables but since I have more than 4000 variables, it's giving error.

I was wondering if anyone has better solutions or idea to resolve this problem ?

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You do not have to use macro logic to generate code. You can use regular SAS code.

Assume you have dataset named CONTENTS with the output from PROC CONTENTS for your variables.

filename code temp;

data _null_;

  set contents ;

  file code ;

  temp = cats('_',_n_);

  put temp '=input(' name ',' length 5. '.);'

     / 'rename ' temp '=' name ';'

     / 'drop ' name ';'

  ;

  if label ne ' ' then put

      'label  ' temp '=' label :$quote. ';'

;

run;

data want ;

  set have ;

  %inc temp;

run;

Message was edited: Fixed DROP statement.

View solution in original post

6 REPLIES 6
jakarman
Barite | Level 11

What is your situation, exactly?   4000 variables looks to be a lot there must be some source of it.  Rows / cols ?

Where is your data stores SAS or in RDBMS?

Are there limitiations that are precision related (flosting point) is binning an approach?
 

The approach of working with arrray-s can be popssible modified and improved for your situation. 

---->-- ja karman --<-----
Sas_R
Calcite | Level 5

The original source is txt file, I loaded that into SAS but now I need to convert the variables to numeric

Vince28_Statcan
Quartz | Level 8

overcome the issue of macro var length by just using proc sql; with macro facility to create many variables instead

proc sql;

     select name, quote(trim(label))

     into :vname1-:vname9999, :vlabel1-:vlabel9999 /*It is possible you may need to break this down into 2 select statements, I don't remember if into statement supports range over multiple macro variables */

     from sashelp.vcolumn

     where libname = "WORK" and

               memname = "HAVE" and

               type = "char"

               /* and any other condition to discriminate the "right" character variables */

     ;

quit;

%macro magic();

data want;

     set have;

     %do i=1 %to &sqlobs;

     new_&&vname&i.. = input(&&vname&i.. , best32.);

     %end;

  

     label

     %do i=1 %to &sqlobs;

          new_&&vname&i.. = &&vlabel&i..

     %end;

     ; /* closing label statements' semi column */

     /* add another do loop to drop &&vname&i.. once you are satisfied with test results */

run;

%mend;

%magic();

Vincent

Tom
Super User Tom
Super User

You do not have to use macro logic to generate code. You can use regular SAS code.

Assume you have dataset named CONTENTS with the output from PROC CONTENTS for your variables.

filename code temp;

data _null_;

  set contents ;

  file code ;

  temp = cats('_',_n_);

  put temp '=input(' name ',' length 5. '.);'

     / 'rename ' temp '=' name ';'

     / 'drop ' name ';'

  ;

  if label ne ' ' then put

      'label  ' temp '=' label :$quote. ';'

;

run;

data want ;

  set have ;

  %inc temp;

run;

Message was edited: Fixed DROP statement.

data_null__
Jade | Level 19

Don't you need to drop NAME not TEMP.

data a;
   a = '123';
   b = input(a,
f3.1);
   rename b=a;
   drop a;
   run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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