SAS十六进制解码器

时间:2017-07-13 14:34:57

标签: matlab sas

以下是我需要转换为SAS的matlab函数。 “价值”的第一行显示为“SUBID_IN_HEX”。

DATA WORK.QUERY_FOR_GETS_DW_EOA_FAULTS;
    SET WORK.QUERY_FOR_GETS_DW_EOA_FAULTS;
    SUBID_IN_HEX = SUBID ;
    FORMAT SUBID_IN_HEX $hex.;

RUN;

'mask'和'start_bit_pos'是由9个元素组成的数组。我是sas的新手,我很欣赏一些关于如何进行的好建议。

祝你好运, 史蒂夫

function decode = get_TABS_bitpack_decoded(hex)

value = hex2dec(hex);
mask = [1 2 4 8 16 hex2dec('1fe0') hex2dec('fe000') hex2dec('3f00000') ...
    hex2dec('fc000000')]; 
start_bit_pos = [0:5 13 20 26];
for i = 1:length(mask)
    temp(i) = bitand(mask(i),value);
    decode(i) = bitshift(temp(i),-start_bit_pos(i));
end
decode = decode(:);

我在下面有一个更新的代码版本。我在数组解码{9}后得到错误124。评论部分显示了一个蛮力方法,说明了这个概念。 do循环是最终目标。

再次感谢您提供的有用评论。

%macro decoderloop(SUBID_IN_HEX);
    call missing(decod);
    array mask{9} (1 2 4 8 16 8160 1040384 66060288 4227858432);
    array start_bit_pos{9} (0:5 13 20 26);
    array decod{9} (0 0 0 0 0 0 0 0 0);

    /*

    temp = band(mask[1],SUBID_IN_HEX);
    decod[1] = BRSHIFT(temp,start_bit_pos[1]);
    temp = band(mask[2],SUBID_IN_HEX);
    decod[2] = BRSHIFT(temp,start_bit_pos[2]);
    temp = band(mask[3],SUBID_IN_HEX);
    decod[3] = BRSHIFT(temp,start_bit_pos[3]);
    temp = band(mask[4],SUBID_IN_HEX);
    decod[4] = BRSHIFT(temp,start_bit_pos[4]);
    temp = band(mask[5],SUBID_IN_HEX);
    decod[5] = BRSHIFT(temp,start_bit_pos[5]);
    temp = band(mask[6],SUBID_IN_HEX);
    decod[6] = BRSHIFT(temp,start_bit_pos[6]);
    temp = band(mask[7],SUBID_IN_HEX);
    decod[7] = BRSHIFT(temp,start_bit_pos[7]);
    temp = band(mask[8],SUBID_IN_HEX);
    decod[8] = BRSHIFT(temp,start_bit_pos[8]);
    temp = band(mask[9],SUBID_IN_HEX);
    decod[9] = BRSHIFT(temp,start_bit_pos[9]);*/
    %do i = 1 %to 9;
        temp = band(mask[&i],SUBID_IN_HEX);
        decod[&i] = BRSHIFT(temp,start_bit_pos[&i]);
    %end;
%mend decoderloop;

1 个答案:

答案 0 :(得分:0)

我的Matlab非常生疏,但我相信以下内容通过创建子程序调用get_TABS_bitpack_decoded来执行您所需要的操作,该调用返回一个水平数组,可以在接收数据步骤中进行转换。

proc fcmp outlib=work.func.conv;
    function hex2dec(hex $);
        return (inputn(hex,cats('hex',lengthn(hex),'.')));
    endsub;

    subroutine get_TABS_bitpack_decoded(hex $, out[9]);
        outargs out;
        value = hex2dec(hex);
        array mask[9] /nosymbols (1 2 4 8 16 1fe0x 0fe000x 3f00000x /*...*/ 0fc000000x);
        array start_bit_pos[9] /nosymbols (0 1 2 3 4 5 13 20 26);
        file log;
        do i=1 to dim(mask);
            temp=band(mask[i],value);
            out[i]=brshift(temp,start_bit_pos[i]);
        end;
    endsub;
quit;

options cmplib=work.func;

data work.query_for_gets_dw_eoa_faults;
    input subid @@;
    SUBID_IN_HEX = put(subid,hex16.);
    array _decode[9] _temporary_;
    call get_TABS_bitpack_decoded(SUBID_IN_HEX,_decode);
    do i=1 to dim(_decode);
      decode=_decode[i];
      output;
    end;
datalines;
8160
;
run;

proc print data=work.query_for_gets_dw_eoa_faults;
    id SUBID;
    var i decode;
run;
subid | i | decode
----- | - | ------
 8160 | 1 |      0
 8160 | 2 |      0
 8160 | 3 |      0
 8160 | 4 |      0
 8160 | 5 |      0
 8160 | 6 |    255
 8160 | 7 |      0 
 8160 | 8 |      0 
 8160 | 9 |      0
%macro decoderloop(var);
    *call missing(decod);*this here causes intantiation or decod as scalar not array;
    array mask[9]          _temporary_ (1 2 4 8 16 8160 1040384 66060288 4227858432);
    array start_bit_pos[9] _temporary_ (0:5 13 20 26);
    array decod[9];
    call pokelong(repeat(put(0,rb8.),8),addrlong(decod[1]),72);*initialize values of decod to all 0, replaces bad call missing and array defaults which do not reinitialize upon data step iteration;
    do _n_=1 to 9;*no macro loop, loop in data step instead of generating repeated code;
      decod[_n_]=brshift(band(mask[_n_],&var),start_bit_pos[_n_]);
    end;
%mend;
data foo;
    SUBID_IN_HEX=8160;
    %decoderloop(SUBID_IN_HEX)
run;