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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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