全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1137 2
2013-04-18
原题:

2. We have data for thetransaction history (in ascending order of date) of bank account.  The bank account number is given in the firstdata line.  The date in the second dataline is the date when the account is opened (it is 05JAN2008 in the following sample data).

001-9878-652817-2

05JAN2008  5,000

02JAN2009  7,500

11NOV2009-5,100

08FEB2010   -950

20MAR2011  4,500

16JAN2012-2,770

09JUN2012   -670

25FEB201312,500

Write one DATA step toread the above data and generate the following report in the Output window.  Interest is computed using the formula

Interest = last balance*((1.000005)**(no. of days)-1)

The output format forwithdrawal, deposit, interest and balance is dollar12.2.   Thedate showed after “Generated on” is the date you run the program.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7

               Reportfor account 001-9878-652817-2

               (Generated on     March 27, 2013)

Date         Withdrawal       Deposit     Interest         Balance

------------------------------------------------------------------

05JAN2008                                               $5,000.00

02JAN2009                  $7,500.00        $9.08      $12,509.08

11NOV2009   $5,100.00                     $19.59       $7,428.68

08FEB2010     $950.00                      $3.31       $6,481.98

20MAR2011                  $4,500.00       $13.14      $10,995.12

16JAN2012   $2,770.00                     $16.62       $8,241.74

09JUN2012     $670.00                      $5.98       $7,577.71

25FEB2013                  $12,500.00        $9.90      $20,087.61

Use the following ruler to readthe column number of the above output.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7

(Hint: Thefirst two data lines should be handled differently from the others.  One method to do so is presented below:

     Read first record;

     <some SAS statements>

     Read second record;

     <some SAS statements>

     DO WHILE (1);

       Read next record;

       <some SAS statements>

     END;

SAS will terminate when all data lines are read.  )


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-4-18 02:20:40
我的代码:
DATA _NULL_;
        FILE PRINT;
        RETAIN K(0);
        K=K+1;
        LENGTH ACCOUNT_NUM $ 17;
        IF K=1 THEN DO;
                INPUT ACCOUNT_NUM;
                PUT @16 'Report for account ' ACCOUNT_NUM;
                PUT;
                GENERATED_DATE = TODAY();
                PUT @17 '(Generated on' @31 GENERATED_DATE WORDDATE.  ')';
                PUT;
                PUT 'Date' @14 'Withdrawal' @31 'Deposit' @43 'Interest' @60 'Balance';
                PUT '------------------------------------------------------------------';
        END;
        ELSE DO;
                LENGTH STR_AMOUNT $ 10;
                RETAIN LAST_DATE(0) BALANCE;
                INPUT DATE DATE9. +1 AMOUNT COMMA6.;
                PUT DATE DATE9. @@;
                IF LAST_DATE=0 THEN DO;
                        PUT @57 AMOUNT DOLLAR10.2;
                        BALANCE=AMOUNT;
                END;
                ELSE DO;
                        INTEREST=BALANCE*(1.000005**(DATE-LAST_DATE)-1);
                        BALANCE=BALANCE+AMOUNT+INTEREST;
                        IF AMOUNT<0 THEN DO;
                                AMOUNT=-1*AMOUNT;
                                PUT @13 AMOUNT DOLLAR10.2 @41 INTEREST DOLLAR10.2 @57 BALANCE DOLLAR10.2;
                        END;
                        ELSE DO;
                                PUT @28 AMOUNT DOLLAR10.2 @41 INTEREST DOLLAR10.2 @57 BALANCE DOLLAR10.2;
                        END;
                END;
                LAST_DATE=DATE;
        END;
CARDS;
001-9878-652817-2
05JAN2008  5,000
02JAN2009  7,500
11NOV2009 -5,100
08FEB2010   -950
20MAR2011  4,500
16JAN2012 -2,770
09JUN2012   -670
25FEB2013 12,500
;
RUN;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-4-18 02:26:19
做这个题时,我主要卡在两个问题上
第一,如何一行一行地读取,即是把第一行和其他行区分开。后来想到利用retain语句,让一个变量作为标记。
第二,这是一个细节问题。原题要求右对齐的格式(帖子上面因为字体及空格处理的原因看不出格式了)。我开始是利用format语句先进行格式化,然后再打印出来,数据一律左对齐了。而且不好调整。后来,我上网查到,put 语句中直接格式化,数值型变量都是右对齐的。于是进行了更正,果然数据右对齐了。
结论:format语句有它的优势,也有它的不足之处。

本人菜鸟,对SAS的理解不深刻,希望大家能够提出宝贵意见,谢谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群