BookmarkSubscribeRSS Feed
wernie
Quartz | Level 8

I have a few datasets where I have a temporal aggregation variable formatted (see screenshot showing temporal aggregation of "5-Year Period"). I then use the following to combine multiple datasets:

 

data final;

    set Output_: ; /* There are 4 datasets in this case, so to combine Output_1 through Output_4 */

run;

 

After I do that, I then lose the format (see screenshot now showing temporal aggregation _5YearPeriod_). I did try to add a line in the above code to say format Temp_Agg $Temp_Agg.; but that did not change the format.

 

I also tried to apply that format in a separate data step to the final dataset and that did not work. Thanks!

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

Could you please provide the format you used to apply in the final dataset. Please provide the code and decode values. Probably the code of the format does not match with the final dataset variables due to which the decode values of the format are not getting applied.

Thanks,
Jag
wernie
Quartz | Level 8

The format looks like this:

 

proc format;
value $Temp_Agg "CT_Case_Year1"="Annual"
"CT_Case_Year2"="Annual"
"CT_Case_Year3"="Annual"
"CT_Case_Year4"="Annual"
"CT_Case_Year5"="Annual"
"CT_Case_Year6"="Annual"
"CT_Case_Year7"="Annual"
"CT_Case_Year8"="Annual"
"CT_Case_Year9"="Annual"
"CT_Case_Year10"="Annual"
"_3YearPeriod_1"="3-Year Period"
"_3YearPeriod_2"="3-Year Period"
"_3YearPeriod_3"="3-Year Period"
"_3YearPeriod_4"="3-Year Period"
"_3YearPeriod_5"="3-Year Period"
"_5YearPeriod_1"="5-Year Period"
"_5YearPeriod_2"="5-Year Period"
"_10YearPeriod"="10-Year Period";
run;

 

Sorry, I'm not sure what you mean by the decode values, so I don't know what to provide there. When I'm looking at the first dataset (where it does apply the format properly), it's showing _5YearPeriod_1 and gets formatted to 5-Year Period. Thank you!

ballardw
Super User

Is that format definition stored in a permanent library or your work library?

If in a permanent library is that library currently defined and in the FMTSEARCH path?

If in the work library, did you run the code to create the format in the current session?

 

There would be no need to use a separate data step:

data final;
    set Output_: ; 
    format Temp_Agg $Temp_Agg. ; 
run;

 

If the format isn't working the most likely thing is that the format is not available at the time you are using it for one of the reasons I asked about.

 

Formats created with basic:

Proc format;

value <remaining format name value lists>;

;

are placed by default into your WORK library and need to have the code run for each session.

Or place the format into a permanent library with the LIBRARY= option. To use the format in later sessions that library need to be defined and the options for the FMTSEARCH path have to include that library.

wernie
Quartz | Level 8

@ballardw It's stored in my work library - I can see it there and know I ran it in the current session. What you have listed for the code is what I tried and it didn't work, which is why I'm confused because I thought that should work.

Reeza
Super User
It should work so either the code is wrong ( you didn't show the actual code you ran) or something else is the issue. Usually either you have the format statement in the wrong place or your log has another error that you didn't catch. Can you post the actual code you ran and the log?
Tom
Super User Tom
Super User

It is not at all clear what you are talking about and the pictures pasted into the attached PDF don't really help to clarify either.

What does PROC CONTENTS show for the original input datasets and the resulting output dataset for the variable in question?

Is it really defined as character?  If so does it have a format attached?  If so what format?

 

If you set together multiple datasets that all have the same variable then the format that is attached by default is the first one seen.

 

Try this program to test.   Change the order of the datasets in the SET statement and check how that impacts which format (if any) ends up being attached the the variable.

 

data one;
  length x $10;
  format x ;
run;

data two ;
  length x $10;
  format x $10.;
run;

data three ;
  length x $10;
  format x $char10.;
run;

data final;
  set one two three;
run;

proc contents data=final; run;
wernie
Quartz | Level 8

Thanks everyone! I got it sorted out! The log had a warning about 'Multiple lengths were specified for the variable Temp_Agg by input data set(s). This can cause truncation of data.'

 

I went to an earlier macro step and set it so all datasets would have the same length for the Temp_Agg variable and that then allowed it to apply that format properly.

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
  • 7 replies
  • 666 views
  • 0 likes
  • 5 in conversation