2016-01-15 116 views
-2
date store  day 
1/1/2013 1  0 
3/2/2013 1  60 
4/5/2015 1  764 
4/5/2003 2  0 
6/7/2003 2  63 
8/1/2004 2  421 
1/1/2005 2  153 
4/5/2003 3  0 
6/5/2003 3  60 

嗨,我有一个如上所述的数据集。给出了Date和Store列的数据。我需要计算一天的专栏。预期的结果现在显示。将Java代码转换为SAS

我在java中有这样的算法,但我不知道如何在SAS中编写它。请帮忙。

//A denotes the date. 
day[0] = 0 
for(int k = 1; k < LEN; i++) { 
    if(store[k] == store[k-1]){ 
     day[i] = A[i ] - A[i-1]; 
    } else { 
     day[k] = 0 
    } 
} 

回答

0

,如果你可以读取数据到豆然后排序& &比较,最后一个是设置 一天,我的代码:

import java.beans.PropertyDescriptor; 
import java.io.Serializable; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
import java.util.concurrent.ConcurrentHashMap; 

/** 
* @author heyunxia ([email protected]) 
* @version 1.0 
* @since 2016-01-15 上午10:30 
*/ 
public class SaS { 

    public static final SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy"); 

    public static void main(String[] args) throws ParseException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { 
     List<DataSet> list = new ArrayList<>(); 

     list.add(new DataSet(sdf.parse("1/1/2013"), 1)); //0 
     list.add(new DataSet(sdf.parse("3/2/2013"), 1)); //60 
     list.add(new DataSet(sdf.parse("4/5/2015"), 1)); //764 
     list.add(new DataSet(sdf.parse("4/5/2003"), 2)); //0 
     list.add(new DataSet(sdf.parse("6/7/2003"), 2)); //63 
     list.add(new DataSet(sdf.parse("8/1/2004"), 2)); //421 
     list.add(new DataSet(sdf.parse("1/1/2005"), 2)); //153 
     list.add(new DataSet(sdf.parse("4/5/2003"), 3)); //0 
     list.add(new DataSet(sdf.parse("6/5/2003"), 3)); //60 


     Map<Integer, List<DataSet>> map = list2GroupedMap(list, "store"); 
     System.out.println(map); 

     Set<Integer> keySet = map.keySet(); 
     Iterator<Integer> iterator = keySet.iterator(); 
     while (iterator.hasNext()) { 
      Integer key = iterator.next(); 
      List<DataSet> dataSetList = map.get(key); 
      DataSet[] dataSets = new DataSet[dataSetList.size()]; 
      dataSetList.toArray(dataSets); 
      Arrays.sort(dataSets); 
      setDays(dataSets); 

     } 

     System.out.println(map); 

    } 

    private static final int DEFAULT_DAYS = 0; 

    //calculate set DataSet property day 
    private static void setDays(DataSet[] dataSets) { 

     int i = 0; 
     DataSet preDataSet = dataSets[i]; 
     preDataSet.setDay(DEFAULT_DAYS); // first one default was 0 

     DataSet curDataSet; 
     for (i = 1; i < dataSets.length; i++) {//skip first one 
      curDataSet = dataSets[i]; 
      curDataSet.setDay(daysBetween(preDataSet.getDate(), curDataSet.getDate())); 

      preDataSet = curDataSet; 
     } 
    } 


    public static int daysBetween(Date preData, Date curData) { 
     Calendar cal = Calendar.getInstance(); 
     cal.setTime(preData); 
     long preTime = cal.getTimeInMillis(); 
     cal.setTime(curData); 
     long curTime = cal.getTimeInMillis(); 
     long betweenDays = (curTime - preTime)/(1000 * 3600 * 24); 

     return Integer.parseInt(String.valueOf(betweenDays)); 
    } 


    private static <T> T getProperty(String propertyName, Object object) { 
     try { 

      PropertyDescriptor pd = new PropertyDescriptor(propertyName, object.getClass()); 
      Method method = pd.getReadMethod(); 
      return (T) method.invoke(object); 
     } catch (Exception ex) { 
      throw new RuntimeException(); 
     } 
    } 

    /** 
    * 
    * 
    * @param objList 
    * @param propertyName 
    * @param <E> 
    * @param <T> 
    * @return 
    * @throws IllegalAccessException 
    * @throws InvocationTargetException 
    * @throws NoSuchMethodException 
    */ 
    public static <E, T> Map<E, List<T>> list2GroupedMap(List<T> objList, String propertyName) throws IllegalAccessException, 
      InvocationTargetException, NoSuchMethodException { 
     if (objList == null || objList.size() == 0) 
      throw new IllegalArgumentException("invalid argument objList"); 
     if (propertyName == null || "".equals(propertyName)) { 
      throw new IllegalArgumentException("invalid argument propertyName"); 
     } 
     Map<E, List<T>> map = new ConcurrentHashMap<>(); 

     for (int i = 0; i < objList.size(); i++) { 
      T obj = objList.get(i); 
      Object propertyValue = getProperty(propertyName, obj); 
      List<T> innerList = map.get(propertyValue); 
      if (innerList == null || innerList.size() == 0) { 
       innerList = new ArrayList<>(); 
      } 
      innerList.add(obj); 
      map.put((E) propertyValue, innerList); 
     } 
     return map; 
    } 
} 

class DataSet implements Serializable, Comparable<DataSet> { 
    private Date date; 
    private Integer store; 
    private Integer day; 

    public DataSet() { 
    } 

    public DataSet(Date date, Integer store) { 
     this.date = date; 
     this.store = store; 
    } 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public Integer getStore() { 
     return store; 
    } 

    public void setStore(Integer store) { 
     this.store = store; 
    } 

    public Integer getDay() { 
     return day; 
    } 

    public void setDay(Integer day) { 
     this.day = day; 
    } 

    @Override 
    public int compareTo(DataSet o) { 
     if (this.date.getTime() > o.date.getTime()) { 
      return 1; 
     } 
     return -1; 
    } 
}