BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ForrestYao
Fluorite | Level 6

Hi there,

 

I have a dataset with multiple variables for fiscal year 2019. One column called Year is a numeric variable eg 2018, 2019 etc. Another column called month is a character variable eg Oct, Nov, Dec etc. I need to generate a new dataset called fiscal year 2020 which contains everything from 2018 November to 2019 October. My patial code is:

proc sql;
create table perm._fydata_2020 as
select * from perm._fydata_2019
where month...(should start from 2018 Nov to 2019 Oct)

 

Because month is a character variable, I am wondering how do I filter a character variable...

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Krueger
Pyrite | Level 9

One way is listing them out individually...

 

WHERE Month in ('string1', 'string2', 'string3')

What you might be able to try to do is setup a sort that has it sorted in the correct order and then rank them. At that point you could filter based off the rank possibly. If you have an example dataset it would be easier to help expand on potential solutions.

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Hi @ForrestYao  Something like the below where expression may work

 

where '01nov2018'd<=input(cats('01',month,year),date9.)<='31oct2019'd
ForrestYao
Fluorite | Level 6

@novinosrin Hi sir, could you explain what does "01" represent inside of CATS function?

novinosrin
Tourmaline | Level 20

First off, to Sir who is that? I am a SAS Janitor. 

 

Okay, basically, I am concatenating a constant day value with '01' as the 1st day of the month. Since your range that you mentioned 2018 NOV-OCT2019. It's safe to assume the whole months starting from 01NOV2019 to 31OCT2019. Assuming you do not have a DATE or DAY variable with values of day of the month, the best bet is to have the 1st day of the month making it inclusive in the specified range.  

 

Of course, you do have the other 2. Therefore the concatenated string would look like 01NOV2018 etc. Then you convert the string to a numeric value using the INFORMAT date9. . I suppose everything else is straight forward plugging in the range within the WHERE clause. Hope this helps!

 

 

 

 


@ForrestYao wrote:

@novinosrin Hi sir, could you explain what does "01" represent inside of CATS function?


 

Krueger
Pyrite | Level 9

One way is listing them out individually...

 

WHERE Month in ('string1', 'string2', 'string3')

What you might be able to try to do is setup a sort that has it sorted in the correct order and then rank them. At that point you could filter based off the rank possibly. If you have an example dataset it would be easier to help expand on potential solutions.

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
  • 4 replies
  • 803 views
  • 0 likes
  • 3 in conversation