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
Onyx | Level 15

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 472 views
  • 2 likes
  • 4 in conversation