BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bhca60
Quartz | Level 8

The values look like this: 202401 for yearmonth and is in numeric format with length 8; how do i convert to character?  Tried doing format but the column comes in blank:

 

 

85 data mmm.mdim_mbr;
86 set mmm.c_mdim
87 mmm.e_mdim
88 mmm.i_mdim
89 mmm.m_mdim;
90
91 format yrmo yymmn6.;
92 yrmo = '';
93
94
95
96
97
98
99 run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
92:18

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you really really really need a character variable in that case it is nearly trivial:

data unwise;
  x= 202401;
  y=put(x,f6.);
run;

But your original value is problematic as it is going to require extra work if you ever want to determine how many intervals are between values, manipulate the value so you determine another date based on the existing one such as the expected end of an activity. Plus there are lots of tools that simplify reporting or modeling on different date intervals such as Year or calendar quarter that are not going to be available with your current values.

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

@bhca60 wrote:

The values look like this: 202401 for yearmonth and is in numeric format with length 8; how do i convert to character?  Tried doing format but the column comes in blank:

 


What possible reason do you have that makes you want the digits 202401 to be characters? My philosophy is that if something represents a date or other calendar quantity, then it should be numeric and it should be converted to a valid SAS date (which is the number of days since 01JAN1960). You seem to want to go in the opposite direction.

--
Paige Miller
bhca60
Quartz | Level 8

Because it displays 202401, 202402, etc.  I will use it in a filter : yrmo > 202401

Does this still need to be converted to sas date?  If so, how would I go about doing that?

PaigeMiller
Diamond | Level 26

Because it displays 202401, 202402, etc.  I will use it in a filter : yrmo > 202401

A filter like

 

if yrmo > 202401 ... ;

need character strings only if yrmo is character. It needs a numeric 202401 if yrmo is numeric. Not different that filters that don't involve dates. If the variable is character then

 

if country='FINLAND' then ...;

works. If the variable is a number, then

 

if dollars>100 then ...;

works as well. The above will not work if dollars is a character variable (which it shouldn't be).

 

Does this still need to be converted to sas date?  If so, how would I go about doing that?

 

In my opinion, this is an excellent habit to get into and saves you lots of trouble and programming in the future. Dates and calendar quantities should be valid SAS dates. Why? Because SAS has done the hard work to create functions and formats and other language elements to work with dates. If they are not valid SAS dates, and you want to know what day it was 91 days ago, this is hard (but not impossible) to program yourself; if you have a SAS date, the hard work is done and all you have to do is subtract 91. In the case of 202401, recently a person at my company was trying to work with this number and determine the previous month, which is 202312; but she couldn't get it to work, all she could do was get 202400. If these are valid SAS dates, SAS knows what the month before 202401 is, you don't have to write code to compute it, instead you tell SAS to compute it from these built-in language elements that already exist to do this. Trivial example:

 

data convert_to_date;
    y=202401; /* Not a valid SAS date */
    /* Convert to a valid SAS date, which is the number of days since 01JAN1960 */
    valid_sas_date_y = input(put(y,6.),yymmn6.); /* Now this is a valid SAS date, the informat yymmn6. does the work */
    format valid_sas_date_y yymmn6.; /* Make it appear readable to humans */
run;

data previous_month;
    set convert_to_date;
    previous_month=intnx('month',valid_sas_date_y,-1); /* Use INTNX to determine previous month */
    format previous_month yymmn6.; /* Make appearance readable to humans */
run;

 

--
Paige Miller
ballardw
Super User

If you really really really need a character variable in that case it is nearly trivial:

data unwise;
  x= 202401;
  y=put(x,f6.);
run;

But your original value is problematic as it is going to require extra work if you ever want to determine how many intervals are between values, manipulate the value so you determine another date based on the existing one such as the expected end of an activity. Plus there are lots of tools that simplify reporting or modeling on different date intervals such as Year or calendar quarter that are not going to be available with your current values.

bhca60
Quartz | Level 8

This worked!  But now, if I want to drop the original and keep the formatted variable how would I do that?

I have this:

data mmm.mdmi_mbr
(rename=(yrmo_old=yrmo_new));
(drop=yrmo_old);
ERROR 180-322: Statement is not valid or it is used out of proper order.
set mmm.mdim_mbr;
run;
PaigeMiller
Diamond | Level 26

 

 

if I want to drop the original and keep the formatted variable how would I do that?

You have written code that has syntax errors. This now has nothing to do with dates.

 

data mmm.mdmi_mbr (rename=(yrmo_old=yrmo_new)); 
(drop=yrmo_old);

 

 

This fails because it places invalid SAS code after the first semi-colon.

 

However this seems like it would work

 

data mmm.mdmi_mbr (drop=yrmo_old);

 

 

--
Paige Miller
bhca60
Quartz | Level 8

Thank you! I decided to use your convert logic and just doing the drop made sense.  THANK YOU!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 787 views
  • 0 likes
  • 3 in conversation