BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

When i run the program, period_1 appear in the date1 dataset, may i know how to output period_2 to period_24 into the date1 dataset? Thanks.


data date1;
date1 = intnx('month',&today,-1,'end');
period_1 = substr(put(date1, yymmddn8.),3,4);
do i=2 to 24;
call symput(cats("Period_",i),substr(put(intnx('month',date1,-(i-1),'end'),yymmddn8.),3,4));
end;
run;

3 REPLIES 3
yabwon
Amethyst | Level 16

Use arrays:

 

%let today=today();
data date1;
  array period_[24] $ 10;
  do i=1 to 24;
    date1 = intnx('month',&today,-i,'end');
    period_[i] = substr(put(date1, yymmddn8.),3,4);
  end;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

@scb wrote:

When i run the program, period_1 appear in the date1 dataset, may i know how to output period_2 to period_24 into the date1 dataset? Thanks.


data date1;
date1 = intnx('month',&today,-1,'end');
period_1 = substr(put(date1, yymmddn8.),3,4);
do i=2 to 24;
call symput(cats("Period_",i),substr(put(intnx('month',date1,-(i-1),'end'),yymmddn8.),3,4));
end;
run;


May I recommend that you avoid macro variables and CALL SYMPUT when data step functions will do the job?

 

May I also recommend that you not store calendar information as characters strings like '1903'? Instead, they should be stored as actual numeric SAS date values and formatted as needed.

 

data date1;
    array period_(24);
    today=today();
    do i=1 to 24;
         period(i) = intnx('month',today,-i,'e');
    end;
    format period_: yymmn4.;   
    drop i today;
run;
        

 

--
Paige Miller
Patrick
Opal | Level 21

@scb 

It's always a bit difficult to really provide an answer just based on code posted but without any sample data (like Have and Want).

Based on your code: Macro level code is great for dynamic code but it's really not the appropriate design to just hold data. Even if things work as desired it will lead to a lot of complicated and unnecessary macro coding.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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