Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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
Sql 如何计算年平均范围?_Sql_Sas - Fatal编程技术网

Sql 如何计算年平均范围?

Sql 如何计算年平均范围?,sql,sas,Sql,Sas,我有一个包含许多字段的数据集。我试图按年份范围的平均值总结“价格”数据。例如: 1900年至1925年:“平均价格” 1925年至1950年:“平均价格” 1950年至1975年:“平均价格” 1975年至2000年:“平均价格” 2000年至2017年:“平均价格” 尝试: proc sql; select avg(price) as avg_price FROM summary WHEN year between 1995 and 2000; quit; 上面的代码不起作用。你能帮我处

我有一个包含许多字段的数据集。我试图按年份范围的平均值总结“价格”数据。例如:

  • 1900年至1925年:“平均价格”
  • 1925年至1950年:“平均价格”
  • 1950年至1975年:“平均价格”
  • 1975年至2000年:“平均价格”
  • 2000年至2017年:“平均价格”
尝试:

proc sql;
select avg(price) as avg_price
FROM summary
WHEN year between 1995 and 2000;
quit;
上面的代码不起作用。你能帮我处理代码吗(请添加到proc并退出,或者我需要的任何东西,我是SAS/SQL新手)


如果你需要一年平均值,那么你需要一年一组

select year, avg(price) as avg_price
FROM summary
WHERE  year between 1995 and 2000
group by year;
或者,对于客户来说,一个简单的方法就是建立联盟

   select  'from 1940 to 1960', avg(price)
   from summary
   WHERE  year between 1940 and 1960
   union 
   select  'from 1960 to 1980', avg(price)
   from summary
   WHERE  year between 1960 and 1980
   union 
   select  'from 1980 to 2000', avg(price)
   from summary
   WHERE  year between 1980 and 2000

如果你需要一年平均值,那么你需要一年一组

select year, avg(price) as avg_price
FROM summary
WHERE  year between 1995 and 2000
group by year;
或者,对于客户来说,一个简单的方法就是建立联盟

   select  'from 1940 to 1960', avg(price)
   from summary
   WHERE  year between 1940 and 1960
   union 
   select  'from 1960 to 1980', avg(price)
   from summary
   WHERE  year between 1960 and 1980
   union 
   select  'from 1980 to 2000', avg(price)
   from summary
   WHERE  year between 1980 and 2000

您得到的错误似乎表明变量year是字符串而不是数字。下面的转换应该会有所帮助

  PROC SQL;
    SELECT mean(price) as average FROM have 
    WHERE 1995 <= input(year,8.) <= 2000 ;
  quit;
PROC-SQL;
从have中选择平均值(价格)作为平均值

其中1995您得到的错误似乎表明变量年份是字符串而不是数字。下面的转换应该会有所帮助

  PROC SQL;
    SELECT mean(price) as average FROM have 
    WHERE 1995 <= input(year,8.) <= 2000 ;
  quit;
PROC-SQL;
从have中选择平均值(价格)作为平均值

其中,在SQL中,您可以通过
语句或计算变量(aka列)对
进行分组。SQL中的平均函数是
MEAN

按计算列分组的示例:

data have;
  do date = '01jan1900'd to '31dec2020'd;
    year = year(date);
    yearChar = put(year,4.);
    price = exp ((date - '01jan1940'd) / (365*12) );
    output;
  end;
  format date yymmdd10.;
run;

proc sql;
  create table want as 
  select
    case 
      when year between 1900 and 1924 then '1900 to 1924'
      when year between 1925 and 1949 then '1925 to 1949'
      when year between 1950 and 1974 then '1950 to 1974'
      when year between 1975 and 1999 then '1975 to 1999'
      when year between 2000 and 2017 then '2000 to 2017'
      else 'out of range'
    end
    as years
  , mean (price) as average_price
  from have
  group by years
  having years not in ('out of range')
;
将创建一个数据集,如

years ($12)     average_price (double)
1900 to 1924       0.120
1925 to 1949       0.967
1950 to 1974       7.777
1975 to 1999      62.546
2000 to 1917     345.873
对于
year
变量为字符类型的情况,需要将该值转换为数字,并在涉及数字的
between
表达式中使用转换后的值

例如:

YearChar
是一个名为的字符列,包含年份值。
input
函数将字符串转换为数值(如果可能)。问号
在转换失败时(例如,当年份为
***
未知时)防止记录消息


在SQL中,您可以通过
语句或计算变量(aka列)对
进行分组。SQL中的平均函数是
MEAN

按计算列分组的示例:

data have;
  do date = '01jan1900'd to '31dec2020'd;
    year = year(date);
    yearChar = put(year,4.);
    price = exp ((date - '01jan1940'd) / (365*12) );
    output;
  end;
  format date yymmdd10.;
run;

proc sql;
  create table want as 
  select
    case 
      when year between 1900 and 1924 then '1900 to 1924'
      when year between 1925 and 1949 then '1925 to 1949'
      when year between 1950 and 1974 then '1950 to 1974'
      when year between 1975 and 1999 then '1975 to 1999'
      when year between 2000 and 2017 then '2000 to 2017'
      else 'out of range'
    end
    as years
  , mean (price) as average_price
  from have
  group by years
  having years not in ('out of range')
;
将创建一个数据集,如

years ($12)     average_price (double)
1900 to 1924       0.120
1925 to 1949       0.967
1950 to 1974       7.777
1975 to 1999      62.546
2000 to 1917     345.873
对于
year
变量为字符类型的情况,需要将该值转换为数字,并在涉及数字的
between
表达式中使用转换后的值

例如:

YearChar
是一个名为的字符列,包含年份值。
input
函数将字符串转换为数值(如果可能)。问号
在转换失败时(例如,当年份为
***
未知时)防止记录消息


我想你的意思是1920-1924,1925-1930,等等,所以每年计算一次

您可以使用
分组依据
和算术:

proc sql;
    select floor(year / 5) * 5 as from_year, 
           avg(price) as avg_price
    from summary
    group by floor(year / 5);   
quit;
如果您还想要年底:

proc sql;
    select floor(year / 5) * 5 as from_year, 
           floor(year / 5) * 5 + 4 as to_year, 
           avg(price) as avg_price
    from summary
    group by floor(year / 5);   
quit;

我想你的意思是1920-1924,1925-1930,等等,所以每年计算一次

您可以使用
分组依据
和算术:

proc sql;
    select floor(year / 5) * 5 as from_year, 
           avg(price) as avg_price
    from summary
    group by floor(year / 5);   
quit;
如果您还想要年底:

proc sql;
    select floor(year / 5) * 5 as from_year, 
           floor(year / 5) * 5 + 4 as to_year, 
           avg(price) as avg_price
    from summary
    group by floor(year / 5);   
quit;

我如何才能得到相同代码中的每个年份范围?例如:1995年至2000年:平均2000年至2005年:平均等更新您的问题添加一个清晰的数据样本,并将您的预期结果作为表格数据..在您的代码中,我收到错误:错误180-322:语句无效或使用顺序不正确。抱歉,我是SAS新手。我必须在代码开头输入proc sql还是在代码结尾退出/运行?回答更新。。在哪里(不是什么时候)以及对自定义范围的建议如何获得相同代码中的每年范围?例如:1995年至2000年:平均2000年至2005年:平均等更新您的问题添加一个清晰的数据样本,并将您的预期结果作为表格数据..在您的代码中,我收到错误:错误180-322:语句无效或使用顺序不正确。抱歉,我是SAS新手。我必须在代码开头输入proc sql还是在代码结尾退出/运行?回答更新。。1925年、1950年、1975年和2000年的价格在哪里(不是什么时候)以及在建议定制范围的情况下,对一个或两个范围有贡献?换句话说,从1900年到1925年(不包括1925年)的价格数据来看,1900年到1925年的平均价格范围是多少?当代码“不起作用”时,您在日志中看到了什么错误?只需复制错误消息的文本并将其粘贴到问题中即可。不需要发布文本的照片。是否必须使用SQL?数据步骤和过程平均数或过程格式+过程平均数更容易。1925年、1950年、1975年和2000年的价格是否构成一个或两个范围?换句话说,从1900年到1925年(不包括1925年)的价格数据来看,1900年到1925年的平均价格范围是多少?当代码“不起作用”时,您在日志中看到了什么错误?只需复制错误消息的文本并将其粘贴到问题中即可。不需要发布文本的照片。是否必须使用SQL?数据步骤和过程方式或过程格式+过程方式更容易。