2016-05-09 25 views
0

現在、私はsasのプロジェクトに取り組んでいます。 2015年11月のデータに基づいてレポートを作成しています。 11月のデータに// consumer accounting/Reports/SAS Reports/20151130/M0ED1という名前のファイルをインポートする必要があります。来月は//消費者会計/ Reports/SAS Reports/20151231/M0ED2になり、来月は20160130とM0ED3のデータを持つ代わりに同じものになります。 私は、レポートを準備しようとするたびに手動でファイル名を書き込む必要がないように、正しい名前でファイルを直接インポートするコードを記述しようとしています。 私に方法を示唆してください。あなたの助けに感謝します。動的日付のSASコード

答えて

0

ここにあなたが行く...

/*Set How many months ago the data you are looking at is*/ 
%let numMonths = 6; 
/*Set which Year and Month was report 1*/ 
%let firstReport = '15NOV2015'd; 


data _null_; 
/*Get Todays Date*/ 
format mydate myreportdate date9.; 
myDate = today(); 
/*Go Back the number of months set in the Macro variable. 
    As you want the last day of that month, add back on 1 month and then remove a day*/ 
myReportDate = intnx('month',myDate,-&numMonths.+1)-1; 
/*Calculate which report number this is using the INTCK Function to count the number of months ends crossed between 2 dates*/ 
myRunNumber = compress(put(INTCK('MONTH',&firstReport.,myReportDate)+1,best12.)); 
/*Create A String with the location and name of your report*/ 
myReportString = "//consumer accounting/Reports/SAS Reports/"|| 
         put(year(myReportDate),z4.)|| 
         put(month(myReportDate),z2.)|| 
         put(day(myReportDate),z2.)|| 
         "/M0ED"||myRunNumber; 
/*Create a macro variable with the location and name of your report*/ 
call symputx('myReport',myReportString); 
run; 

/*Output the macro variable to the log*/ 
%put &myReport.; 
+0

は答えをありがとうございました。しかし、まだ私には1つの問題があります。私がmydate = today()を使うと、もう1つはM0EOD1を必要に応じて実行します。私がmydate = '15Jun2016'dを置くと、私はM0EOD2を望みどおりに得ますが、mydate = '15Jul2016'dとすると、M0EOD91が得られますが、私はM0EOD3を期待していました。私がmydate = '15Aug2016'dを置くと、私はM0EOD92を得る。誰が問題がどこにあるのか教えていただけますか? – shankar

+0

こんにちはシャンカー、私はこの答えを書いたとき私は私の前にSASを持っていなかったので、間違いがうかがった!私は2つの日付の差を計算するintck関数を代わりに使用するように自分のコードを編集しました。この場合、月ごとの差を計算するように設定しました。これであなたの質問に答えることができれば私に緑のダニを与えてください! –