阅读单个记录,拆分为多个观察但保留一个值

时间:2018-02-13 14:04:36

标签: sas

我在一个记录中为过去12个月的客户提供了结算数据。客户ID是一个6位数字。

108263 $946.00 $903.00 $804.00 $674.00 $663.00 $195.00 $922.00 $595.00
> $157.00 $415.00 $868.00 $750.00

我需要将数据分解为以下格式。注意,我还必须显示需要自动计算的月份(月份数和月份名称)。

Cust_ID | Month.N | Month.W|Bill Amt
108263|1|Jan|$946.00
108263|2|Feb|$903.00
...
108263|12|Dec|$750.00

3 个答案:

答案 0 :(得分:1)

data have;
input Cust_ID $ @@;
do i=1 to 12;
   Month_N=i;
   Month_W=substr(put(intnx('month','01Jan2018'd,i-1),Date9.),3,3);
   input Bill_Amt $ @@;
   output;
end;
drop i;
cards; 
108263 $946.00 $903.00 $804.00 $674.00 $663.00 $195.00 $922.00 $595.00 $157.00 $415.00 $868.00 $750.00
;

答案 1 :(得分:0)

如果数据在SAS数据集中,则有一种方法可以执行此操作

data want;
set have;
cust_id =scan(text,1,' ');
do i = 2 to 13;
month_n = i -1;
if month_n = 1 then month_w = 'Jan';
if month_n = 2 then month_w = 'Feb';
if month_n = 3 then month_w = 'Mar';
if month_n = 4 then month_w = 'Apr';
if month_n = 5 then month_w = 'May';
if month_n = 6 then month_w = 'Jun';
if month_n = 7 then month_w = 'Jul';
if month_n = 8 then month_w= 'Aug';
if month_n = 9 then month_w = 'Sep';
if month_n = 10 then month_w = 'Oct';
if month_n = 11 then month_w = 'Nov';
if month_n = 12 then month_w = 'Dec';
billamount = input(scan(text, i, ' '),  COMMA10.2);
output;
end;
drop i text;
format billamount dollar10.2;
run;

答案 2 :(得分:0)

如果您正在从文本文件中读取数据,则信息NLMNLUSDw.d可以解析货币值。

创建示例文本文件以从

中读取
filename have temp;

data _null_;
  file have;
  put "108263 $946.00 $903.00 $804.00 $674.00 $663.00 $195.00 $922.00 $595.00 $157.00 $415.00 $868.00 $750.00";
  put "108264 $946.00 $903.00 $804.00 $674.00 $663.00 $195.00 $922.00 $595.00 $157.00 $415.00 $868.00 $750.00";
  put "108265 $946.00 $903.00 $804.00 $674.00 $663.00 $195.00 $922.00 $595.00"; * some missing months;
run;

示例:

NLMNLUSDw.d Informat读取波多黎各和美国本地表达式的货币格式。当在语句之前指定变量属性时,此处input语句最有效。

data want;
  infile have missover;
  attrib
    id length=8 
    month_num length=8 
    month_name3 length=$3 
    amount length=8 informat=NLMNLUSD9.2 format=DOLLAR9.2
  ;
  input id @;
  do month_num = 1 to 12;  * presume each row has 12 months of data;
    month_name3 = put(mdy(month_num,1,2000), monname3.);
    input amount @;
    output;
  end;
run;