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

Hi I want to create "July 2nd Week" in new column,Below is the sample code i tried but i stuck in adding ('th' 'st') dynamically  or is there any alternative way to create please help me on this .

 

data want;
format Today date9. month $3. Week $3. Cat $10.;
Today=today();
month=put(month(Today),monname3.);
Week=put(WEEKDAY(Today),weekday.);
Cat= put(catx(' ',month,Week,'Week'),$10.);
run;

 

Thanks,

Siva

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

I have an ordinal format here you can use:

 

https://gist.github.com/statgeek/6c885f6aea18dbd6caf8

 

data ordinal_format;
	length label $8.;
  do number=1 to 300;
    numstring = put(number, 16. -l);
    if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
    else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
    else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
    else ending = 'th';
    ordstring =catt(numstring, ending);

	  start=number;
	  label=ordstring;
	  fmtname="ordinal_fmt";
	  type="N";
	  output;
  end;


	keep start label fmtname type;
run;

proc format cntlin=ordinal_format;
run;

data want;
do i =1 to 100;
	j=i;
	output;
end;

format j ordinal_fmt.;
run;

@sivastat08 wrote:

Hi I want to create "July 2nd Week" in new column,Below is the sample code i tried but i stuck in adding ('th' 'st') dynamically  or is there any alternative way to create please help me on this .

 

data want;
format Today date9. month $3. Week $3. Cat $10.;
Today=today();
month=put(month(Today),monname3.);
Week=put(WEEKDAY(Today),weekday.);
Cat= put(catx(' ',month,Week,'Week'),$10.);
run;

 

Thanks,

Siva


 

View solution in original post

2 REPLIES 2
Reeza
Super User

I have an ordinal format here you can use:

 

https://gist.github.com/statgeek/6c885f6aea18dbd6caf8

 

data ordinal_format;
	length label $8.;
  do number=1 to 300;
    numstring = put(number, 16. -l);
    if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
    else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
    else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
    else ending = 'th';
    ordstring =catt(numstring, ending);

	  start=number;
	  label=ordstring;
	  fmtname="ordinal_fmt";
	  type="N";
	  output;
  end;


	keep start label fmtname type;
run;

proc format cntlin=ordinal_format;
run;

data want;
do i =1 to 100;
	j=i;
	output;
end;

format j ordinal_fmt.;
run;

@sivastat08 wrote:

Hi I want to create "July 2nd Week" in new column,Below is the sample code i tried but i stuck in adding ('th' 'st') dynamically  or is there any alternative way to create please help me on this .

 

data want;
format Today date9. month $3. Week $3. Cat $10.;
Today=today();
month=put(month(Today),monname3.);
Week=put(WEEKDAY(Today),weekday.);
Cat= put(catx(' ',month,Week,'Week'),$10.);
run;

 

Thanks,

Siva


 

Kurt_Bremser
Super User

Create a custom format:

proc format;
value myxx
  1 = '1st'
  2 = '2nd'
  3 = '3rd'
  4 = '4th'
  5 = '5th'
  6 = '6th'
;
run;

This should suffice, as a month can't stretch over more than six weeks.

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1759 views
  • 2 likes
  • 3 in conversation