Help using Base SAS procedures

Concatenate date variable to create headers in dataset

Reply
Frequent Contributor
Posts: 131

Concatenate date variable to create headers in dataset

data _NULL_;

call symput('c_dt',input(&currdate_sql,yymmdd10.));

call symput ( 'qtr_start_1', put(intnx('quarter',&c_dt,-1,'b'),date9.));

     call symput ( 'qtr_end_1', put(intnx('quarter',&c_dt,-1,'e'),date9.));

     call symput ( 'qtr_start_2', put(intnx('quarter',&c_dt,-2,'b'),date9.));

     call symput ( 'qtr_end_2', put(intnx('quarter',&c_dt,-2,'e'),date9.));

     call symput ( 'qtr_start_3', put(intnx('quarter',&c_dt,-3,'b'),date9.));

     call symput ( 'qtr_end_3', put(intnx('quarter',&c_dt,-3,'e'),date9.));

     call symput ( 'qtr_start_4', put(intnx('quarter',&c_dt,-4,'b'),date9.));

     call symput ( 'qtr_end_4', put(intnx('quarter',&c_dt,-4,'e'),date9.));

run;

 

data quarters;

set group12;

 

if datepart(report_dt_p) GE intnx('month', "&trend_end."d /*&trend_end.*/, -2) then Q1_count=count;

else Q1_count=0;

 

if datepart(report_dt_p) GE intnx('month', "&trend_end."d /*&trend_end.*/, -5) and datepart(report_dt_p)

LT intnx('month',"&trend_end."d /*&trend_end.*/, -2) then Q2_count=count; else Q2_count=0;

 

if datepart(report_dt_p) GE intnx('month', "&trend_end."d /*&trend_end.*/, -8) and datepart(report_dt_p)

LT intnx('month', "&trend_end."d /*&trend_end.*/, -5) then Q3_count=count;else Q3_count=0;

 

if datepart(report_dt_p) LT intnx('month', "&trend_end."d /*&trend_end.*/, -8) then Q4_count=count;

else Q4_count=0;

if quarter='Q1' then Q1_count=count; else Q1_count=0;

if quarter='Q2' then Q2_count=count; else Q2_count=0;

if quarter='Q3' then Q3_count=count; else Q3_count=0;

if quarter='Q4' then Q4_count=count; else Q4_count=0;

run;

sample data

Q1_count

Q2_count

Q3_count

Q4_count

0

1

2

1

6

19

9

81

3

5

0

1

10

9

0

0

1

6

5

2

Here is my question.  Is there a way to assign the various Q counts a header based on the quarter.  For example Q1_Count becomes Q1_Count_”&qtr_start_1” ||&qtr_end_1”  or Q1_count_1jul2015-30Sep2015

Super User
Posts: 17,784

Re: Concatenate date variable to create headers in dataset

Deal with your data in a vertical (long) format and then transpose it, using ID and IDLABEL to automatically create your variable name/headers.
Super User
Posts: 5,079

Re: Concatenate date variable to create headers in dataset

Do you understand what the program is doing?  In particular, do you understand SAS dates, INTNX, and CALL SYMPUT?  If so, it should be easy enough.  (If not, you have some studying to do.)  Add a few more date computations and CALL SYMPUTs to the top DATA step.  They will create macro variables holding the labels you want, and you can assign them as variable labels in the bottom DATA step using a LABEL statement.

 

I wouldn't recommend changing the variable names, just assign labels instead.  It is extremely cumbersome to deal with variable names that contain nonstandard characters such as a dash.  Make sure you instruct the reporting procedure that you use that it should use the labels instead of the variable names as column headings.

 

Good luck.

Super User
Posts: 10,483

Re: Concatenate date variable to create headers in dataset

I have a sneaking suspicion that much of that code isn't needed at all and an appropriate custome picture format with date directives might work just as well. And with counts as the objective possibly using the format on a date variable from your raw data in a report procedure.

Super User
Posts: 5,255

Re: Concatenate date variable to create headers in dataset

I haven't dived into your logic, but it's rarely wise to create measurement columns based on the time dimension.

Instead, create rows for each quarter, and leave the transposing to any succeeding reporting procedure.

Data never sleeps
Ask a Question
Discussion stats
  • 4 replies
  • 363 views
  • 2 likes
  • 5 in conversation