用于从数据值末尾去除撇号的 SAS 代码

发布于 2024-09-10 09:21:10 字数 139 浏览 6 评论 0原文

我有一个变量 Deck1-Deck100(Deck1 到 Deck100 代表任务的 100 次试验),有四个可能的值 A' B' C' D'。我需要重新编码这个变量,以便 A' 和 B' = 0 且 C' 和 D' = 1。你能帮忙吗?我对如何压缩变量一无所知。

I have a variable Deck1-Deck100 (Deck1 through Deck100 representing 100 trials of a task) with four possible values A' B' C' D'. I need to recode this variable so that A' and B' = 0 and C' and D' = 1. Can you help? I know nothing about how to compress variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡離愁欲言轉身 2024-09-17 09:21:10

或信息方法:

/* generate a fake data set */
data trials;
    array trial(100) $2.;
    array t(4) $2. _temporary_ ("A'","B'","C'","D'");
    do i = 1 to 100;
        j=ceil(ranuni(112233)*4);
        trial[i] = t[j];
    end;
    drop i j ;
run;
proc print noobs;
    var trial1-trial10;
run;

/* create a 'recoding' format */
proc format;
    invalue trials (upcase)
        "A'","B'"=0
        "C'","D'"=1
        other=.;
run;

/* convert the values */
data newtrials;
    set trials;
    array trial(*) $2. trial1-trial100;
    array rtrial(100);
    do i = 1 to 100;
        rtrial[i]=input(trial[i], trials.);
    end;
    drop i trial:;
run;
proc print noobs;
    var rtrial1-rtrial10;
run;

产生以下输出:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

trial1    trial2    trial3    trial4    trial5    trial6    trial7    trial8    trial9    trial10

  D'        D'        B'        A'        A'        D'        A'        B'        C'        D'

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

rtrial1   rtrial2   rtrial3   rtrial4   rtrial5   rtrial6   rtrial7   rtrial8   rtrial9   rtrial10

   1         1         0         0         0         1         0         0         1          1

or the informat approach:

/* generate a fake data set */
data trials;
    array trial(100) $2.;
    array t(4) $2. _temporary_ ("A'","B'","C'","D'");
    do i = 1 to 100;
        j=ceil(ranuni(112233)*4);
        trial[i] = t[j];
    end;
    drop i j ;
run;
proc print noobs;
    var trial1-trial10;
run;

/* create a 'recoding' format */
proc format;
    invalue trials (upcase)
        "A'","B'"=0
        "C'","D'"=1
        other=.;
run;

/* convert the values */
data newtrials;
    set trials;
    array trial(*) $2. trial1-trial100;
    array rtrial(100);
    do i = 1 to 100;
        rtrial[i]=input(trial[i], trials.);
    end;
    drop i trial:;
run;
proc print noobs;
    var rtrial1-rtrial10;
run;

which produces this output:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

trial1    trial2    trial3    trial4    trial5    trial6    trial7    trial8    trial9    trial10

  D'        D'        B'        A'        A'        D'        A'        B'        C'        D'

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

rtrial1   rtrial2   rtrial3   rtrial4   rtrial5   rtrial6   rtrial7   rtrial8   rtrial9   rtrial10

   1         1         0         0         0         1         0         0         1          1
围归者 2024-09-17 09:21:10

假设您在一次观察中拥有所有 100 个变量,并且只想重新编码这些值,则以下方法可能有效。只需将数字 4 替换为数字 100。

data sample;

    deck1='A''';
    deck2='B''';
    deck3='C''';
    deck4='D''';
run;

proc print data=sample;
run;

data result;
    set sample;

    array alldecks(4) deck1-deck4;

    do i=1 to 4;
        if alldecks(i) eq 'A''' or alldecks(i) eq 'B''' then alldecks(i)='0';
        if alldecks(i) eq 'C''' or alldecks(i) eq 'D''' then alldecks(i)='1';
        end;

    drop i;
run;

proc print data=result;
run;

这将为您提供以下输出:

Obs    deck1    deck2    deck3    deck4

 1      A'       B'       C'       D'  

Obs    deck1    deck2    deck3    deck4

 1       0        0        1        1  

Assuming that you have all 100 variables in one observation and just want to recode the values, the following might work. Just replace the number 4 with the number 100.

data sample;

    deck1='A''';
    deck2='B''';
    deck3='C''';
    deck4='D''';
run;

proc print data=sample;
run;

data result;
    set sample;

    array alldecks(4) deck1-deck4;

    do i=1 to 4;
        if alldecks(i) eq 'A''' or alldecks(i) eq 'B''' then alldecks(i)='0';
        if alldecks(i) eq 'C''' or alldecks(i) eq 'D''' then alldecks(i)='1';
        end;

    drop i;
run;

proc print data=result;
run;

This gives you the following output:

Obs    deck1    deck2    deck3    deck4

 1      A'       B'       C'       D'  

Obs    deck1    deck2    deck3    deck4

 1       0        0        1        1  
情深缘浅 2024-09-17 09:21:10

剥离单引号很容易,只需 compress() 即可:

data _null_;
  length old new $8.;
  old = "A'";
  new = compress(old, "'");
  put (_all_) (=/);
run;
/* on log
old=A'
new=A
*/

Striping the single quote is easy, just compress() it out:

data _null_;
  length old new $8.;
  old = "A'";
  new = compress(old, "'");
  put (_all_) (=/);
run;
/* on log
old=A'
new=A
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文