BookmarkSubscribeRSS Feed
Solly7
Pyrite | Level 9

Hi, i need to help with below..i need to get only year and month form below data

 

Data Have               Data Want

190125                    201901

180512                    201805

171202                    201712

6 REPLIES 6
Tom
Super User Tom
Super User

Is HAVE a character string or a number? If it is a number is the value 190,125 or are they actual date values that have been formatted with YYMMDD6. format?   

If they are not real date values why are you sure the first one should be 2019 and not 1919  or 2119?

PeterClemmensen
Tourmaline | Level 20

Are your 'have' values numeric or character?

doctortimi
Obsidian | Level 7

With the assumption that "HAVE' is a numeric variable, you can apply the yymmn6. format.

 

 

andreas_lds
Jade | Level 19

@doctortimi wrote:

With the assumption that "HAVE' is a numeric variable, you can apply the yymmn6. format.

 

 


No, you can't. Dates are the number of days since 1Jan1960:

25         data _null_;
26            a = 190125;
27            put a=;
28            put a= date9.;
29            put a= yymmn6.;
30         run;

a=190125
a=17JUL2480
a=248007
doctortimi
Obsidian | Level 7

Thanks for the correction. I wrongly assumed that HAVE were the days since 1960.

mkeintz
PROC Star

You don't specify whether your starting value is a character string or number:

 

If it is a character string:

 

data have;
  input strng $6.;
datalines;
190125
180512
171202
run;
data want;
  set have;
  dat=input(strng,yymmdd6.);
  format dat yymmn6. ;
  put (_all_) (=);
run;

If it's a number, then:

 

data have;
  input nmbr ;
datalines;
190125
180512
171202
run;
data _null_;
  set have;
  dat=mdy(mod(floor(nmbr/100),100),mod(nmbr,100),2000+floor(nmbr/10000));
  format dat yymmn6.;
  put (_all_) (=);
run;

Note that in both cases, you have to transform the actual value.  It's not just a matter of finding the right format.  BTW, if you run the programs above without assigning a format to dat, you'll see the actual value (number of days after 01jan1960).

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 6 replies
  • 753 views
  • 2 likes
  • 6 in conversation