Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用PROC转置的SAS转置_Sas - Fatal编程技术网

不使用PROC转置的SAS转置

不使用PROC转置的SAS转置,sas,Sas,我有一个关于在不使用PROC转置的情况下转置数据的问题 0 a b c 1 dog cat camel 2 9 7 2534 如果不使用PROC转置,如何获得以下数据集: Animals Weight 1 dog 9 2 cat 7 3 camel 2534 这是一个有点奇怪的要求。此示例代码是为您的3个变量硬编码的。如果需要的话,你将不得不概括这

我有一个关于在不使用PROC转置的情况下转置数据的问题

0   a      b     c
1  dog    cat   camel
2  9      7     2534 
如果不使用PROC转置,如何获得以下数据集:

   Animals       Weight     
1  dog           9
2  cat           7
3  camel         2534

这是一个有点奇怪的要求。此示例代码是为您的3个变量硬编码的。如果需要的话,你将不得不概括这一点

data temp;
input a $ b $ c $; 
datalines;
dog cat camel
9 7 2534
;
run;

data animal_weight;
set temp end=last;
format animal animals1-animals3 $8.;
format weight weights1-weights3 best. ;
retain animals: weights:;
array animals[3];
array weights[3];

if _n_ = 1 then do;
    animals[1] = a;
    animals[2] = b;
    animals[3] = c;
end;
else if _n_ = 2 then do;
    weights[1] = input(a,best.);
    weights[2] = input(b,best.);
    weights[3] = input(c,best.);
end;

if last then do;
    do i=1 to 3;
        animal = animals[i];
        weight = weights[i];
        output;
    end;
end;
drop i animals: weights: a b c;
run;

将值读入2个数组,将字符串的权重转换为数字。使用
\u N\u
变量确定要填充的数组。在数据集的末尾,输出数组中的值

这是一个有点奇怪的请求。此示例代码是为您的3个变量硬编码的。如果需要的话,你将不得不概括这一点

data temp;
input a $ b $ c $; 
datalines;
dog cat camel
9 7 2534
;
run;

data animal_weight;
set temp end=last;
format animal animals1-animals3 $8.;
format weight weights1-weights3 best. ;
retain animals: weights:;
array animals[3];
array weights[3];

if _n_ = 1 then do;
    animals[1] = a;
    animals[2] = b;
    animals[3] = c;
end;
else if _n_ = 2 then do;
    weights[1] = input(a,best.);
    weights[2] = input(b,best.);
    weights[3] = input(c,best.);
end;

if last then do;
    do i=1 to 3;
        animal = animals[i];
        weight = weights[i];
        output;
    end;
end;
drop i animals: weights: a b c;
run;

将值读入2个数组,将字符串的权重转换为数字。使用
\u N\u
变量确定要填充的数组。在数据集的末尾,输出数组中的值

这是一个有点奇怪的请求。此示例代码是为您的3个变量硬编码的。如果需要的话,你将不得不概括这一点

data temp;
input a $ b $ c $; 
datalines;
dog cat camel
9 7 2534
;
run;

data animal_weight;
set temp end=last;
format animal animals1-animals3 $8.;
format weight weights1-weights3 best. ;
retain animals: weights:;
array animals[3];
array weights[3];

if _n_ = 1 then do;
    animals[1] = a;
    animals[2] = b;
    animals[3] = c;
end;
else if _n_ = 2 then do;
    weights[1] = input(a,best.);
    weights[2] = input(b,best.);
    weights[3] = input(c,best.);
end;

if last then do;
    do i=1 to 3;
        animal = animals[i];
        weight = weights[i];
        output;
    end;
end;
drop i animals: weights: a b c;
run;

将值读入2个数组,将字符串的权重转换为数字。使用
\u N\u
变量确定要填充的数组。在数据集的末尾,输出数组中的值

这是一个有点奇怪的请求。此示例代码是为您的3个变量硬编码的。如果需要的话,你将不得不概括这一点

data temp;
input a $ b $ c $; 
datalines;
dog cat camel
9 7 2534
;
run;

data animal_weight;
set temp end=last;
format animal animals1-animals3 $8.;
format weight weights1-weights3 best. ;
retain animals: weights:;
array animals[3];
array weights[3];

if _n_ = 1 then do;
    animals[1] = a;
    animals[2] = b;
    animals[3] = c;
end;
else if _n_ = 2 then do;
    weights[1] = input(a,best.);
    weights[2] = input(b,best.);
    weights[3] = input(c,best.);
end;

if last then do;
    do i=1 to 3;
        animal = animals[i];
        weight = weights[i];
        output;
    end;
end;
drop i animals: weights: a b c;
run;

将值读入2个数组,将字符串的权重转换为数字。使用
\u N\u
变量确定要填充的数组。在数据集的末尾,输出数组中的值

对于一个我真的想获得好成绩的家庭作业问题,我不会给出这个答案(因为它太高级了,所以很明显你需要帮助);但哈希解决方案几乎肯定是最灵活的,我希望在现实世界中这样做的人会这么做(假设现实世界中存在“不使用proc转置”的原因,例如可用资源)。这个问题有点未定义,因此这只是适度的容错

data have;
input a $ b $ c $;
datalines;
dog    cat   camel
9      7     2534 
;;;;
run;

data _null_;
set have end=eof;
array charvars _character_;
if _n_ = 1 then do;
    length animal $15 weight 8;
    declare hash h();
    h.defineKey('row');
    h.defineData('animal','weight');
    h.defineDone();
end;
animal=' ';
weight=.;
do row = 1 to dim(charvars);
  rc_f = h.find();
  if rc_f ne 0 then do;
    animal=charvars[row];
    rc_a = h.add();
    animal=' ';
  end;
  else if rc_f eq 0 then do;
    weight=input(charvars[row],best12.);
    rc_r = h.replace();
  end;
end;
if eof then rc_o = h.output(dataset:'want');
run;

我不会把这个作为一个家庭作业问题的答案,我实际上想在这个问题上取得好成绩(因为它太高级了,所以很明显你需要帮助);但哈希解决方案几乎肯定是最灵活的,我希望在现实世界中这样做的人会这么做(假设现实世界中存在“不使用proc转置”的原因,例如可用资源)。这个问题有点未定义,因此这只是适度的容错

data have;
input a $ b $ c $;
datalines;
dog    cat   camel
9      7     2534 
;;;;
run;

data _null_;
set have end=eof;
array charvars _character_;
if _n_ = 1 then do;
    length animal $15 weight 8;
    declare hash h();
    h.defineKey('row');
    h.defineData('animal','weight');
    h.defineDone();
end;
animal=' ';
weight=.;
do row = 1 to dim(charvars);
  rc_f = h.find();
  if rc_f ne 0 then do;
    animal=charvars[row];
    rc_a = h.add();
    animal=' ';
  end;
  else if rc_f eq 0 then do;
    weight=input(charvars[row],best12.);
    rc_r = h.replace();
  end;
end;
if eof then rc_o = h.output(dataset:'want');
run;

我不会把这个作为一个家庭作业问题的答案,我实际上想在这个问题上取得好成绩(因为它太高级了,所以很明显你需要帮助);但哈希解决方案几乎肯定是最灵活的,我希望在现实世界中这样做的人会这么做(假设现实世界中存在“不使用proc转置”的原因,例如可用资源)。这个问题有点未定义,因此这只是适度的容错

data have;
input a $ b $ c $;
datalines;
dog    cat   camel
9      7     2534 
;;;;
run;

data _null_;
set have end=eof;
array charvars _character_;
if _n_ = 1 then do;
    length animal $15 weight 8;
    declare hash h();
    h.defineKey('row');
    h.defineData('animal','weight');
    h.defineDone();
end;
animal=' ';
weight=.;
do row = 1 to dim(charvars);
  rc_f = h.find();
  if rc_f ne 0 then do;
    animal=charvars[row];
    rc_a = h.add();
    animal=' ';
  end;
  else if rc_f eq 0 then do;
    weight=input(charvars[row],best12.);
    rc_r = h.replace();
  end;
end;
if eof then rc_o = h.output(dataset:'want');
run;

我不会把这个作为一个家庭作业问题的答案,我实际上想在这个问题上取得好成绩(因为它太高级了,所以很明显你需要帮助);但哈希解决方案几乎肯定是最灵活的,我希望在现实世界中这样做的人会这么做(假设现实世界中存在“不使用proc转置”的原因,例如可用资源)。这个问题有点未定义,因此这只是适度的容错

data have;
input a $ b $ c $;
datalines;
dog    cat   camel
9      7     2534 
;;;;
run;

data _null_;
set have end=eof;
array charvars _character_;
if _n_ = 1 then do;
    length animal $15 weight 8;
    declare hash h();
    h.defineKey('row');
    h.defineData('animal','weight');
    h.defineDone();
end;
animal=' ';
weight=.;
do row = 1 to dim(charvars);
  rc_f = h.find();
  if rc_f ne 0 then do;
    animal=charvars[row];
    rc_a = h.add();
    animal=' ';
  end;
  else if rc_f eq 0 then do;
    weight=input(charvars[row],best12.);
    rc_r = h.replace();
  end;
end;
if eof then rc_o = h.output(dataset:'want');
run;

您总是只有两行,还是列数和行数都是动态的

如果您具有动态行数和列数,那么理想的方法是使用open函数,将列数获取到宏变量。这将是新数据集中的行数。然后取原始数据集中的行数,即新数据集中的列数。这必须在实际转置方法之前发生。发布后,您可以将其读入数组,并使用宏变量作为维度将值输出到新数据集


话虽如此,既然SAS已经提供了现成的转置功能,为什么还要重新发明转轮呢?

您总是只有两行,还是列数和行数都是动态的

如果您具有动态行数和列数,那么理想的方法是使用open函数,将列数获取到宏变量。这将是新数据集中的行数。然后取原始数据集中的行数,即新数据集中的列数。这必须在实际转置方法之前发生。发布后,您可以将其读入数组,并使用宏变量作为维度将值输出到新数据集


话虽如此,既然SAS已经提供了现成的转置功能,为什么还要重新发明转轮呢?

您总是只有两行,还是列数和行数都是动态的

如果您具有动态行数和列数,那么理想的方法是使用open函数,将列数获取到宏变量。这将是新数据集中的行数。然后取原始数据集中的行数,即新数据集中的列数。这必须在实际转置方法之前发生。发布后,您可以将其读入数组,并使用宏变量作为维度将值输出到新数据集


话虽如此,既然SAS已经提供了现成的转置功能,为什么还要重新发明转轮呢?

您总是只有两行,还是列数和行数都是动态的

如果您具有动态行数和列数,那么理想的方法是使用open函数,将列数获取到宏变量。这将是新文件中的行数