BookmarkSubscribeRSS Feed
nduksy
Obsidian | Level 7
Hello,
 
I will appreciate your assistance in reviewing the attached code.
Some background - the holiday data has a day and month column (both numeric). I  concatenated them, the concatenated data displays something like this 1-12  (i.e. December 1). 
 
I used the format function to format the numeric months as character months so I can present something such as - 'December 1' in my output.
 
When I run it, the 'Day and Month' column is empty
 
Thank you in anticipation.
 
/*Concatenating Day and Moth*/
data derive.holiday;
	set holiday;
	DayMonth = month ||'-'|| day;
run;

/*Formatting numeric Month to Character Month*/
proc format;
value $CharMonth '1' = 'January'
		 '2' = 'February'
		 '3' = 'March'
		 '4' = 'April'
		 '5' = 'May'
		 '6' = 'June'
		 '7' = 'July'
		 '8' = 'August'
		 '9' = 'September'
		 '10' = 'October'
		 '11' = 'November'
		 '12' = 'December'
		;
run;

/*Applying informat to data*/
data derive.holiday;
	set holiday;
	format month $CharMonth.;
run;

/*Printing Holiday*/
proc printto print ='C:\Users\nduka\Documents\SAS Training\Studies\Clinical\Data\output\HolidayReport.txt' new;
run;

proc report data = derive.holiday nowd headskip split='/';
	title 'List of holidays';
	footnote 'Created by Nduka Oluku';
	column name desc day month DayMonth begin;
	define name/display 'Holiday Name' width=25;
	define desc/display 'Holiday Description' width=30;
	define day/display 'Day' width=3;
	define month/display 'Month' width=5;
	define DayMonth/display 'Day & Month' width=20 format=$CharMonth.;
	define begin/display 'Year Started' width=20;
	break after name /skip;
	compute after;
	line ' ';
	endcomp;
	
run;
8 REPLIES 8
Astounding
PROC Star

Start at the beginning.  Examine the variable DayMonth in your top DATA step.  What do you want it to contain?  Is it character or numeric?  What is its length?  What is its value?  That should narrow down where you need to fix things.

Kurt_Bremser
Super User

In your first step, you create a dataset with variable daymonth.

In the second step, you create a format.

In the third step, you overwrite the dataset you created in the first step, without daymonth.

 

So you can't expect any values in daymonth.

nduksy
Obsidian | Level 7

Thanks for pointing out the issue. Can you advise on what I need to do?

 

Shmuel
Garnet | Level 18

The variables day and month - are they char type or numeric?

Are there any messages in the log?

Can you post the output you got?

 

The order of your steps is not clear - on first step you created a new variable named DayMonth.

On next step (excluding proc format) you override the output dataset delivery.holiday and you lost the DayMonth variable.

 

If you want to save the output report to a file you need to run proc printto twice - the first, as you have done, is to to assign the outfile name. The 2nd should be after proc report and  is to close the file by:

proc printto; run;

 

 

 

nduksy
Obsidian | Level 7

Day and Month are both numeric characters

nduksy
Obsidian | Level 7

I am sorry, they are numeric

ballardw
Super User

First some details you need to provide.

Are your original Day and Month variables character or numeric?

Second you define the format to use values 1 through 12 but then apply them to a value Daymonth that you think is "12-1". Since you do not have the value "12-1" in the format it does not get applied and the value is likely shown. But depending on the format when you create DayMonth you likely have values with spaces due to using the || operator.

Example:

data example;
   day=1;
   month=12;
   daymonth = month||'-'||day;
run;

Which will have an actual value (pasting into a text box because the forum will reformat the text to much shorter)

Daymonth="          12-           1"

because the default format for numeric variables as I created that is BEST12 and the conversion to character that takes place using the || operator uses the default format and right justifies the result. So there are LOTS of spaces, 10 before the 12 and 11 before the 1.

Here is a large economy sized hint: when storing any value that might resemble a date then create an actual date with a year month and day of the month. Then you can create formats that will work a bit more consistently.

 

To create a format that would display "12-1"  "12-2" ... "12-31" you would have to list each value to display December. Ranges work oddly if you try to use them with character values. I'll leave this as an exercise for the interested reader.

 

Another issue is what can happen using character formats with undefined values and default display lengths. Using your format and the data I created above:

proc print data=example;
    title "Simple format use $charmonth.";
   format daymonth $charmonth.;
run; title;

proc print data=example;
    title "use $charmonth20.";
   format daymonth $charmonth20.;
run; title;

You will see that Daymonth does not appear to be in the first proc print output and only appears partially in the second. Using an explicit width with the format, such as $charmonth20. it will force displaying up to 20 characters if available. Since there are lots of spaces between the - and the 1 for days that still isn't wide enough. Sometimes when creating format you need to specify a default display length as well. Follows an example with a date type numeric value and format with directives to display month and day. SAS supplies a format MONNAME that will display any date just as the name, but you do need a complete date value to use it.

data example2;
   day=1;
   month=12;
   date = mdy(12,1,1960);
run;

proc format;
picture Month_day  (default=15)
low-high = '%B-%d' (datatype=date)
;

proc print data=example2;
   format date month_day.;
run;

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1090 views
  • 0 likes
  • 5 in conversation