DATA Step, Macro, Functions and more

Import selective records

Reply
Super Contributor
Posts: 426

Import selective records

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.

Frequent Contributor
Posts: 84

Re: Import selective records

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

Super Contributor
Posts: 426

Re: Import selective records

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. -##
Super User
Posts: 17,936

Re: Import selective records

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

Super Contributor
Posts: 408

Re: Import selective records

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.

Super User
Posts: 9,691

Re: Import selective records

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;


Super Contributor
Posts: 426

Re: Import selective records

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.

Super User
Posts: 9,691

Re: Import selective records

?? 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.
Super Contributor
Posts: 426

Re: Import selective records

I need to filter the records while importing not after importing

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 10,548

Re: Import selective records

[ Edited ]

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.

 

Super User
Posts: 17,936

Re: Import selective records


 

 


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. 

Ask a Question
Discussion stats
  • 10 replies
  • 480 views
  • 4 likes
  • 6 in conversation