BookmarkSubscribeRSS Feed
rykwong
Quartz | Level 8

Hi

I want to remove certain symbols from this list of variables ST2--bsitosrat, and also to rename them into bm1--bm44, so I write this to code.  Problem is array a will not run because it states ST2--bsitosrat contains both numeric and character variables.  Can you suggest a way to do this?  The dataset keep renewing and the numeric or character status of the list change over time which to me is the challenge.  thanks

 

array a[*]

 

ST2 Tchol LPA LPA_mass HCY CYST_C  eGFR bsitos bsitosrat; 

 

array b[*] 

bm1  bm2  bm3  bm4  bm5  bm6  bm7. bm8. bm9; 

 

do i = 1 to dim(b);

a[i]=compress(a[i], "<>"); 

if a[i] in ("See Notes", "Rej") then a[i]=.;

b[i]=1*a[i]; 

end;

10 REPLIES 10
PaigeMiller
Diamond | Level 26

Numeric variables won't contain the symbols that you are trying to remove, so you don't even need to run this code on the numeric variables; just run it on the character variables.

--
Paige Miller
rykwong
Quartz | Level 8
Thanks but the problem is the dataset changes all the time and some variables on the list contains the symbols next week when they do not now. Some variables contains the symbols (thus character) now and may no long contain the symbol (i.e. because numeric)
I am trying to create a code that works over time
rykwong
Quartz | Level 8

the list of variables is actually much larger and I am numeric vs character status changes week by week

 

PaigeMiller
Diamond | Level 26

Then you can use PROC CONTENTS to determine which variables are numeric (denoted as TYPE=1 by PROC CONTENTS) and which variables are character (denoted as TYPE=2 by PROC CONTENTS).

 

proc contents data=have out=_contents_ noprint;
run;

proc sql noprint;
      select distinct name into :charvars separated by ' ' from _contents_ where type=2;
quit;

data want;
    set have;
    array a &varnames;
    array b bm1-bm&sqlobs;
    do i=1 to &sqlobs;
         /* some stuff here */
    end;
run;
    

I don't know if this is an official "maxim" or not, but PROC CONTENTS is a programmer’s best friend.

 

--
Paige Miller
Kurt_Bremser
Super User

@PaigeMiller wrote:

I don't know if this is an official "maxim" or not, but PROC CONTENTS is a programmer’s best friend.

 


Actually, proc contents is part of Maxim 3 (know your data) 😉

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"Thanks but the problem is the dataset changes all the time " - and that is indeed the sole root of your problem, thus fixing that should be highest priority.  Unless you like refactoring each and every run, in which case just do it manually.

novinosrin
Tourmaline | Level 20

Some sample data would help test

Shmuel
Garnet | Level 18

As you can't define numeric and character variables in same array and special symbol are not expected in numeric 

you can:

1) create a new dataset and rename variable using proc dataset.

2) define array of character variables only:

data new;
  set have;  
  array a _character_;
  do i=1 to dim(a);
      a(i) = compress(a(i), '<>');
      if a(i) in ("See Notes" , "Rej") then a(i) = . ;
  end;
run;

proc datasets lib=work;
   modify new;
   rename 
      'ST2'   = 'BM1'
      'TCHOL' = 'BM2'
      'LPA'  =  'BM3'
       .....
  ;
quit;
      

 

Reeza
Super User
This is a problem with your input data. I'm guessing you're reading from an excel file. The variable types in an array must be the same type, so you'll first need to conver tthem all to the same type. In your code some have periods but they shouldn't.
Kurt_Bremser
Super User

@rykwong wrote:
The dataset keep renewing and the numeric or character status of the list change over time which to me is the challenge.

Translation: I'm wading hip-deep in a pile of excrements, and it STINKS!

 

Fix the import process, so you get consistent attributes.

DO NOT USE EXCEL, period. If data has to come from an Excel source, save it to a csv file and read that with a data step.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 1284 views
  • 0 likes
  • 7 in conversation