BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

I've a CSV file which has last one year (Aug'15 to Jul'16) data. Now my requirement is to import only last 3 months (Jul'16 , Jun'16 & May'16) records without hardcoding. File has a date variable which has values like 21JUN2016 , 20JUL2016 , 31DEC2015 alongside other variables.

 

Appreciate if someone guide me with the code.

10 REPLIES 10
kumarK
Quartz | Level 8

can you post some sample have dataset and what columns you want that would be helpful to give better solution. Thanks

Babloo
Rhodochrosite | Level 12
Since it is a interview question I don't have any example dataset. I need
to import all the variables including the key variable date.

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Reeza
Super User

@Babloo Shouldnt you complete interview questions on your own? It's not a fair representation of your skill set otherwise. 

jklaverstijn
Rhodochrosite | Level 12

How about this:

 

proc sql;
    create table want as
        select * from have
        where month(datevar) + 3 > (
           select max(month(datevar) from have));
quit;

Hope this helps,

- Jan.

Ksharp
Super User
You'd better post some data to let us test code:


proc sql;
create table want as
 select *
  from sashelp.air
   having date ge intnx('month',max(date),-2) ;
quit;


Babloo
Rhodochrosite | Level 12

Thanks for the code.

 

Your code subsetting the data from master SAS dataset (sashelp.air). But my question is to creating a master datset with subset of records. i.e. I will need to create a SAS dataset by importing only selective records.

Ksharp
Super User
?? Once you import that CSV file correctly and there is a DATE type variable date , you can use my code ,just replace SASHELP.CLASS with your HAVE dataset.
Babloo
Rhodochrosite | Level 12
I need to filter the records while importing not after importing

##- Please type your reply above this line. Simple formatting, no
attachments. -##
ballardw
Super User

@Babloo wrote:
I need to filter the records while importing not after importing


Good luck with that. If you are supposed to determine the last three months from the contents of the data file without reading it first then I suspect that you may have partially misunderstood the question.

Generically I would have looked at

1) read the data

2) determine the last date in the data set

3) then build a filter and subset the original data.

 

Or possibly the purpose of the question would be to get you to request clarification of the process.

 

Reeza
Super User

 

 


I suspect that you may have partially misunderstood the question.

Interview questions are meant to test both understanding and technical ability so this is a good question according to that. 

And yes, it's being misunderstood.

 

The solution is a single line of code (WHERE clause) in the data input step based on todays date and intnx function. 

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
  • 10 replies
  • 1800 views
  • 4 likes
  • 6 in conversation