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

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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