BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

I am trying to create sas dates parameters.

I see that the reqeusted parameters were not created.

Does anyone know why??

%let Vector1=1806+1805+1803+1712; 

%let mon_now=%scan(&Vector1.,1,+);
%let mon_kdm=%scan(&Vector1.,2,+);
%let qua_kdm=%scan(&Vector1.,3,+);
%let last_dec=%scan(&Vector1.,4,+);
%put &mon_now.;
%put &mon_kdm.;
%put &qua_kdm.;
%put &last_dec.;



data out;
length mon_now_Minus12E_char mon_kdm_Minus12E_char qua_kdm_Minus12E_char last_dec_Minus12E_char $15.;
format mon_now_Minus12E_SAS_D  mon_kdm_Minus12E_SAS_D  qua_kdm_Minus12E_SAS_D  last_dec_Minus12E_SAS_D date9.;
mon_now_Minus12E_SAS_D = intnx('month',input("&mon_now",yymmn4.),-12,'end');
mon_kdm_Minus12E_SAS_D = intnx('month',input("&mon_kdm",yymmn4.),-12,'end');
qua_kdm_Minus12E_SAS_D = intnx('month',input("&qua_kdm",yymmn4.),-12,'end');
last_dec_Minus12E_SAS_D =intnx('month',input("&last_dec",yymmn4.),-12,'end');


mon_now_Minus12E_char = cats(put(mon_now_Minus12E_SAS_D,date9.),"'d");
mon_kdm_Minus12E_char = cats(put(mon_kdm_Minus12E_SAS_D,date9.),"'d");
qua_kdm_Minus12E_char = cats(put(qua_kdm_Minus12E_SAS_D,date9.),"'d");
last_dec_Minus12E_char =cats(put(last_dec_Minus12E_SAS_D,date9.),"'d");


CALL SYMPUT('mon_now_Minus12E',mon_now_Minus12E_char);
CALL SYMPUT('mon_kdm_Minus12E',mon_kdm_Minus12E_char);
CALL SYMPUT('qua_kdm_Minus12E',qua_kdm_Minus12E_char);
CALL SYMPUT('last_dec_Minus12E',last_dec_Minus12E_char);
run;
%put &mon_now_Minus12E.;  /* I don't get it here!!!*/
%put &mon_kdm_Minus12E.; /* I don't get it here!!!*/
%put &qua_kdm_Minus12E.; /* I don't get it here!!!*/
%put &last_dec_Minus12E.; /* I don't get it here!!!*/
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

You have imbalanced quotes. 

Is that on purpose?

If so be careful when you use the macro variables.

This works:

 

%put %superq(mon_now_Minus12E );
%put %superq(mon_kdm_Minus12E );
%put %superq(qua_kdm_Minus12E );
%put %superq(last_dec_Minus12E);


30JUN2017'd
31MAY2017'd
31MAR2017'd
31DEC2016'd

 

 

 

View solution in original post

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

You have imbalanced quotes. 

Is that on purpose?

If so be careful when you use the macro variables.

This works:

 

%put %superq(mon_now_Minus12E );
%put %superq(mon_kdm_Minus12E );
%put %superq(qua_kdm_Minus12E );
%put %superq(last_dec_Minus12E);


30JUN2017'd
31MAY2017'd
31MAR2017'd
31DEC2016'd

 

 

 

hashman
Ammonite | Level 13

Ronein,

 

You under-parameterize; and as a result, end up with rather unwieldy code burdened with excessive hard coding. Instead, you'd be better off storing things you're likely to change in the future in parameters and then let SAS do its job. An extra benefit is that you'd end up with much cleaner and comprehensible code:

 

%let V = 1806+1805+1803+1712              ; * yymm date list ;          
%let M = 12                               ; * -number months ;          
%let P = mon_now mon_kdm qua_kdm last_dec ; * mvar prefixes  ;          
%let S = Minus&M.E                        ; * mvar suffix    ;          
%let F = date9.                           ; * mvar format    ;          
                                                                        
data _null_ ;                                                           
  do j = 1 to countw ("&V", "+") ;                                      
    mm = intnx ("mon", input (scan ("&V", j, "+"), yymmn4.), -&M, "E") ;
    q9 = cats ("'", put (mm, &F), "'d") ;                               
    mv = catx ("_", scan ("&P", j), "&S") ;                             
    call symputx (mv, q9) ;                                             
  end ;                                                                 
run ;  

%put _user_ ;

To realize the advantage of this approach, imagine that you had a list of 20 YYMM encoded dates and 20 corresponding names for the macro variables to populate (instead of just 4). In this case, you'd have to add 80 lines to your existing code, while the program above wouldn't swell by a single line. 

 

Paul D.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1031 views
  • 1 like
  • 3 in conversation