SAS检查年份是否为IN字符

时间:2021-03-17 20:14:20

I'm new to SAS and trying to do the next: I have two tables (users & reviews) connected through user_id.

我是SAS的新手,并试图做下一个:我有两个表(用户和评论)通过user_id连接。

I've merged both via user_id. There are two variables I want to use:

我通过user_id合并了两个。我想要使​​用两个变量:

  1. "date" (of the review. Saved as format MMDDYY10.)
  2. “日期”(评论。保存为格式MMDDYY10。)

  3. "elite" (year a user was elite, saved as a character)
  4. “精英”(一年级用户是精英,保存为角色)

Elite has different forms (examples):

Elite有不同的形式(例子):

  • empty cell
  • 2012,2015,2017
  • 2012:2017

How do I check if a user was elite when he wrote the review?

如何在撰写评论时检查用户是否是精英?

Thanks in advance, Peyo

提前谢谢,Peyo

2 个解决方案

#1


0  

This covers the case when a year is not present in the text of elite but is included in a range:

这包括精英文本中不存在一年但包含在范围内的情况:

data test_cases;
input date yymmdd10. + 1 elite $32.;
format date yymmdd10.;
infile cards missover;
cards;
2012-01-01 2012,2015,2017
2012-01-01 2012:2017
2013-01-01 2012:2017
2012-01-01
2011-01-01 2012,2015,2017
2011-01-01 2012:2017
;
run;

data example;
  set test_cases;
  article_year = put(year(date),4.);
  if index(elite,':') = 0 then elite_flag = index(elite,article_year);
  else elite_flag = substr(elite,1,4) <= article_year <= substr(elite,6,9);
run;

#2


0  

You can use the YEAR function to extract the year from date, then use the PUT function to convert it to character and finally the INDEX function to search the elite list:

您可以使用YEAR函数从日期中提取年份,然后使用PUT函数将其转换为字符,最后使用INDEX函数搜索精英列表:

if index(elite,put(year(date),4.)) then ...

The condition will be true when the index function founds the year of date within the elite variable.

当索引函数在精英变量中找到日期年份时,条件将成立。

#1


0  

This covers the case when a year is not present in the text of elite but is included in a range:

这包括精英文本中不存在一年但包含在范围内的情况:

data test_cases;
input date yymmdd10. + 1 elite $32.;
format date yymmdd10.;
infile cards missover;
cards;
2012-01-01 2012,2015,2017
2012-01-01 2012:2017
2013-01-01 2012:2017
2012-01-01
2011-01-01 2012,2015,2017
2011-01-01 2012:2017
;
run;

data example;
  set test_cases;
  article_year = put(year(date),4.);
  if index(elite,':') = 0 then elite_flag = index(elite,article_year);
  else elite_flag = substr(elite,1,4) <= article_year <= substr(elite,6,9);
run;

#2


0  

You can use the YEAR function to extract the year from date, then use the PUT function to convert it to character and finally the INDEX function to search the elite list:

您可以使用YEAR函数从日期中提取年份,然后使用PUT函数将其转换为字符,最后使用INDEX函数搜索精英列表:

if index(elite,put(year(date),4.)) then ...

The condition will be true when the index function founds the year of date within the elite variable.

当索引函数在精英变量中找到日期年份时,条件将成立。