Search code examples
arraysvariablessasmacros

SAS scan variable's label and then change the variable's value


Good morning!

I have a situation that needs some help.

I have a dataset with 300 variables, almost all of them are numeric var. The data source is an Excel spreadsheet. About 100 variables in the spreadsheet are something like "Percent of ...." or "... percent ...".

But the value is not decimal, instead, is a numeric value after multiple by 100. I need to change the value to a decimal. For example, the value now in the dataset is 53, but actually, it is 0.53.

My thinking is to first scan the variable's label if there is "Percent" or "percent" in there, if so, store this variable in an array. Then have a do loop to read the array and change the corresponding value by being divided by 100.

I did the codes as the following:

 proc contents data=dataset1 noprint out=__cont;
   run;

   proc sql;
    select count(libname) into: maxcount
    from __cont
    ;
   quit;

   %put &maxcount;

   data output;
    set __cont;
    array percentvar{&maxcount} $;
    do i=1 to &maxcount;
        if scan(upcase(label), 'PERCENT')>1 then percentvar(i)=name;
    end;
    drop i;
   run;

But actually, the array has a dynamic dimension. This is the first error, then I am not sure how to connect the variable with the array and then change its value.

I need help! or some logic clarification.

Thank you so much for any input and thoughts!

Appreciated! Lin


Solution

  • No need for code generation for this project. You can use the VLABEL() function to find the label.

    data output;
      set dataset1 ;
      array nums _numeric_;
      do index=1 to dim(nums);
        if findw(vlabel(nums[index]),'percent',,'i') then 
          nums[index]=nums[index]/100 
        ;
      end;
      drop index;
    run;