0
votes

I have 10 csv files that would like to do PROC IMPORT, one at each time, depending on the value user input. For example, user will input 201801 to run January 2018 csv file(PROC IMPORT).

For that case, i tried to use substring &period(which stores the yymmn6. value which user input) and then from there, put in proc import. My code as below:

   %let period=201812;
%macro convertdate();
data convertdate;
year=substr("&period",1,4);
month=substr("&period",5,2);
if month='01' then newmonth='jan';
if month='02' then newmonth='feb';
if month='03' then newmonth='mac';
if month='04' then newmonth='apr';
if month='05' then newmonth='may';
if month='06' then newmonth='jun';
if month='07' then newmonth='jul';
if month='08' then newmonth='aug';
if month='09' then newmonth='sep';
if month='10' then newmonth='oct';
if month='11' then newmonth='nov';
if month='12' then newmonth='dec';

run;

/*Assign convertdate the only distinct record into macro, then set as the data step for proc import*/
%if month='01' %then %do;
    %let newmonth=jan;
%end;



/*proc import and remaining transformation from existing script*/
proc import out=rmr_raw_source
  file="/sasdata/source/user_files/re_&newmonth.2018.xlsx"

  dbms=xlsx replace;
  sheet="re_&newmonth.2018";
  getnames=no;
  dbsaslabel=none;
run;

%mend;
%convertdate;

However, it wont work. I am getting warning below:

WARNING: Apparent symbolic reference NEWMONTH not resolved.

Does anyone have better solution to it?

1
The Month-variable exists inside the data-set. You need to create Global variable. You could try call symput: if month='01' then call symput('newmonth','jan'); More in documentation: support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/…pinegulf

1 Answers

0
votes

You can try something as follows:

%let period = '201801';                                     /*7*/
%global month;
data test123;
   period = .                                        /*1*/

      full_date = cats(period,'01');                        /*2*/
      format converted_date DATE10.;                        /*3*/
      converted_date = input(full_date, yymmdd8.);          /*4*/
      month = put(converted_date,monname3.);                /*5*/
      call symput('month',month);                           /*6*/
  run;
  1. Create a SAS variable out of Macro.
  2. Set to first date of the given month.
  3. Create a new var and set the date format.
  4. convert the TEXT date to SAS date.
  5. get the month name out of date created above.
  6. create a macro variable named 'month' out of SAS variable month.
  7. Make macro variable 'month' global.

You can now use month macro variable anywhere in the code.