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

I have written some code, and it works just fine.  I am always trying to learn other ways to do the same thing. In this case, instead of relying on PROC SQL and into: for the macro variable I would like to learn another way to accomplish this and possibly lessen the number of lines of code I am writing.

 

Here is what I currently have:  CD=CalendarDay, PM=PriorMonth, CM=CurrentMonth

 

DATA DATES;
    FORMAT START_DATE_PM DATE9.;
    FORMAT CD3PM DATE9.;
    FORMAT START_DATE_CM DATE9.;
    FORMAT CD2CM DATE9.;
    START_DATE_PM = INTNX('MONTH',TODAY(),-1,'B');
    CD3PM = INTNX('DAY',START_DATE_PM,2);
    START_DATE_CM = INTNX('MONTH',TODAY(),0,'B');
    CD2CM = INTNX('DAY',START_DATE_CM,1);
RUN;

PROC SQL NOPRINT;
    SELECT  CD2CM, CD3PM
    INTO:   CD2CM,
        :   CD3PM
    FROM    DATES;
QUIT;
%PUT &CD2CM;
%PUT &CD3PM;
1 ACCEPTED SOLUTION

Accepted Solutions
CurtisMackWSIPP
Lapis Lazuli | Level 10

I'm not understanding your question.  What do you want to do with the values of CD2CM & CD3PM?

If you just want to set those macro variables in a more straight forward way, you could do this.

 

%let START_DATE_PM = %sysfunc(INTNX(MONTH,%sysfunc(TODAY()),-1,B));
%let CD3PM %sysfunc(putn(%sysfunc(INTNX(DAY,&START_DATE_PM,2)),DATE9.));
%let START_DATE_CM = %sysfunc(INTNX(MONTH,%sysfunc(TODAY()),0,B));
%let CD2CM %sysfunc(putn(%sysfunc(INTNX(DAY,&START_DATE_CM,1)),DATE9.));

%PUT &CD2CM;
%PUT &CD3PM;  

View solution in original post

6 REPLIES 6
CurtisMackWSIPP
Lapis Lazuli | Level 10

I'm not understanding your question.  What do you want to do with the values of CD2CM & CD3PM?

If you just want to set those macro variables in a more straight forward way, you could do this.

 

%let START_DATE_PM = %sysfunc(INTNX(MONTH,%sysfunc(TODAY()),-1,B));
%let CD3PM %sysfunc(putn(%sysfunc(INTNX(DAY,&START_DATE_PM,2)),DATE9.));
%let START_DATE_CM = %sysfunc(INTNX(MONTH,%sysfunc(TODAY()),0,B));
%let CD2CM %sysfunc(putn(%sysfunc(INTNX(DAY,&START_DATE_CM,1)),DATE9.));

%PUT &CD2CM;
%PUT &CD3PM;  
SAS_ACE
Obsidian | Level 7
Hi Curtis. This is definitely the "shortest" way. Thank you for looking at this.

SASKiwi
PROC Star

How about this? CALL SYMPUTX will do what you need.

DATA DATES;
    FORMAT START_DATE_PM DATE9.;
    FORMAT CD3PM DATE9.;
    FORMAT START_DATE_CM DATE9.;
    FORMAT CD2CM DATE9.;
    START_DATE_PM = INTNX('MONTH',TODAY(),-1,'B');
    CD3PM = INTNX('DAY',START_DATE_PM,2);
    START_DATE_CM = INTNX('MONTH',TODAY(),0,'B');
    CD2CM = INTNX('DAY',START_DATE_CM,1);
    call symputx ('CD3PM', CD3PM);
    call symputx ('CD2CM', CD2CM);
RUN;

BTW, coding in mixed case is far easier to read.

SAS_ACE
Obsidian | Level 7
SASKiwi,

That is close, but when i do the %PUT on the macro variables they return days from 1960. How can we change to show in DATE9. ?
CurtisMackWSIPP
Lapis Lazuli | Level 10
DATA DATES;
    START_DATE_PM = INTNX('MONTH',TODAY(),-1,'B');
    CD3PM = INTNX('DAY',START_DATE_PM,2);
    START_DATE_CM = INTNX('MONTH',TODAY(),0,'B');
    CD2CM = INTNX('DAY',START_DATE_CM,1);

    call symput("CD3PM",put(CD3PM,DATE9.));
    call symput("CD2CM",put(CD2CM,DATE9.));
RUN;

%PUT &CD2CM;
%PUT &CD3PM;
SAS_ACE
Obsidian | Level 7

Ah, very nice.

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
  • 6 replies
  • 2030 views
  • 2 likes
  • 3 in conversation