DATA Step, Macro, Functions and more

Importing sas7bdat file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Importing sas7bdat file

I have a sas7bdat file placed on my local drive (CSmiley Happy. How do i make sas recognize it for further use? I have used Proc import (by giving the location path for the file) and have used Set statement as well the same way, but am getting massage which reads as:-

Error:- Found "<Pathname to the file>" when expecting a name

Am I doing something wrong here?


Accepted Solutions
Solution
‎02-08-2014 10:36 AM
SAS Super FREQ
Posts: 8,743

Re: Importing sas7bdat file

Hi:

  You should NOT need to import a SET statement or DATA= option should be sufficient, if you have the syntax correct. Let's assume the physical name and location of your file is:

c:\mydata\info.sas7bdat

Which means that the file is stored in a physical location (c:\mydata) on your machine and the name of the file is info.sas7bdat.

Next you will need to "plug into" the data -- just like you plug your lamp into the electrical outlet -- the lamp won't turn on until is has power. Your SAS session can't read data until is knows where to look for data. So now, you need a LIBNAME statement:

libname wombat 'c:\mydata';

The LIBNAME statement establishes a "nickname" or libref (library reference) that gives SAS a high leval name to use. In this case, it is something silly like WOMBAT, but it can be any name, as long as the name conforms to the SAS naming conventions (which I leave for you to look up in the documentation).

The LIBNAME statement will provide your SAS session with the high level location of the data. Any SAS datasets in that location will now be accessible to you. But there's one more thing. You have to use a 2 level name to point to the individual dataset within the library. Pointing to the library is only good for some procedures:

proc contents data=wombat._all_;

  title 'What datasets are in this location?';

run;

To make a copy of info.sas7bdat or to use it in a procedure, you would need to use the 2 level name do this:

data new;

  set wombat.info;

  newvar = 'something';

run;

proc print data=wombat.info;

  title 'Original wombat.info file';

run;

proc print data=work.new;

  title 'After copying the file and creating NEWVAR';

run;

but, you do not have to use the LIBNAME.DATA convention. That is how most SAS programs are written. So it would be good for you to understand what a SAS library is and how it is used in a 2 level name. You can, of course, just use the fully qualified name in code...so this should work for you (it worked for me):

data new;

  set 'c:\mydata\info.sas7bdat';

run;

  

proc print data='c:\mydata\info.sas7bdat';

run;

This program now has to be changed in every location where the physical file name is referenced. With my version of the code, using the LIBNAME statement, I only have to change the LIBNAME statement in order to be able to point to a changed location for the data. Maintenance of code should always be a factor in how you write your code (in my opinion).

cynthia

View solution in original post


All Replies
Solution
‎02-08-2014 10:36 AM
SAS Super FREQ
Posts: 8,743

Re: Importing sas7bdat file

Hi:

  You should NOT need to import a SET statement or DATA= option should be sufficient, if you have the syntax correct. Let's assume the physical name and location of your file is:

c:\mydata\info.sas7bdat

Which means that the file is stored in a physical location (c:\mydata) on your machine and the name of the file is info.sas7bdat.

Next you will need to "plug into" the data -- just like you plug your lamp into the electrical outlet -- the lamp won't turn on until is has power. Your SAS session can't read data until is knows where to look for data. So now, you need a LIBNAME statement:

libname wombat 'c:\mydata';

The LIBNAME statement establishes a "nickname" or libref (library reference) that gives SAS a high leval name to use. In this case, it is something silly like WOMBAT, but it can be any name, as long as the name conforms to the SAS naming conventions (which I leave for you to look up in the documentation).

The LIBNAME statement will provide your SAS session with the high level location of the data. Any SAS datasets in that location will now be accessible to you. But there's one more thing. You have to use a 2 level name to point to the individual dataset within the library. Pointing to the library is only good for some procedures:

proc contents data=wombat._all_;

  title 'What datasets are in this location?';

run;

To make a copy of info.sas7bdat or to use it in a procedure, you would need to use the 2 level name do this:

data new;

  set wombat.info;

  newvar = 'something';

run;

proc print data=wombat.info;

  title 'Original wombat.info file';

run;

proc print data=work.new;

  title 'After copying the file and creating NEWVAR';

run;

but, you do not have to use the LIBNAME.DATA convention. That is how most SAS programs are written. So it would be good for you to understand what a SAS library is and how it is used in a 2 level name. You can, of course, just use the fully qualified name in code...so this should work for you (it worked for me):

data new;

  set 'c:\mydata\info.sas7bdat';

run;

  

proc print data='c:\mydata\info.sas7bdat';

run;

This program now has to be changed in every location where the physical file name is referenced. With my version of the code, using the LIBNAME statement, I only have to change the LIBNAME statement in order to be able to point to a changed location for the data. Maintenance of code should always be a factor in how you write your code (in my opinion).

cynthia

N/A
Posts: 1

Re: Importing sas7bdat file

Hi Everyone, let's beef up this question a bit :

What if my SAS dataset is on server A and SAS 9.3 (im using EG to connect to it) is running on server B and for SAS to see folders and files on server A , it needs to use pc file server ( installed on a window server i think with office on it)  ? how can i import the .sasbat file from server A to server B using SAS? server A is windows base and server B is unix base.

Welcome to my nightmare !

Contributor
Posts: 25

Re: Importing sas7bdat file

Dear Cynthia,

I am a newbie in SAS community and I really need help to convert a variable from numeric (Format BEST12) to a date format in SAS. I am writing to you because I really like the easy explanations you give to users in the community. Many SAS experts usually write codes in a difficult way- they use abbreviations and a gergon I really find difficult to understand. I hope you can help me!

I have a dataset that I imported in SAS from a sas7bdat file. Now I have a variable called "Month" that is a date and it is displayed in the following format:

201403
201404
201405
201406

And so on for million of  rows! 

Now SAS reads this variable as being Numeric - Format BEST12 -. And I need SAS to display it as a date - YYMMDD.10 Format - and not as a Numeric variable - BEST12 Format-.

Any suggestion? 

Thank you!

SAS Super FREQ
Posts: 8,743

Re: Importing sas7bdat file

Hi:

  Although I will answer your question here, my first recommendation is that you do not add a NEW question on a NEW topic to an old posting. In fact, this old posting was about trying to use PROC IMPORT on a SAS dataset that already existed, which is a totally different question than the one you have. When I opened the post, I almost closed it again because the posting was so old -- and I had to search through it to find why it had popped up again. Your post got buried in the middle of an old post. It would be better to start a completely new posting.

 

  You did well, to provide a sample of your data, however. But, think of poor SAS, when storing the number 201403, how is SAS to know that the number 201403 means March 2014 and NOT $201,403???

 

  SAS stores date values in a special way, as the number of days offset from a zero date -- the zero date in SAS is Jan 1, 1960 -- so think of a timeline like this:

dates_offset_from_0.png

 

So your "number" of 201403 isn't ready to be treated like a SAS date variable Because for SAS, the number 201403 is a lot of days AFTER Jan 1, 1960 -- up into the 2500's --

 

  Let's look at a timeline with more days on it (not to scale):

more_days.png

  When you want SAS to treat a numeric variable differently, you need to take your date and "find" the correct number of days since Jan 1, 1960 -- going forward in time (positive number) or backward in time (negative number), as shown above. So, SAS would store July 4, 1776 as -67019 and then you could use ANY date format to display the date in any form you chose. You can prove this to yourself by a simple program like this:

 

data testit;
  date1a = mdy(07,04,1776);
  date1b = -67019;
  date2 = -3334;
  date3 = -368;
  date4 = 0;
  date5 = 9;
  date7 = 9099;
  date8 = 19783;
  date9 = 201403;
run;
  
proc print data=testit;
  title '1) internally stored date values';
run;
  
proc print data=testit;
  title '2 Use a format';
  format date: yymmdd10.;
run;

and the results of that program are:

after_testit.png

 

  So as you can see in the program above -- you can figure out the number of days since Jan 1, 1960 and make your numeric date variable, or you can use the MDY function (one of many date functions) to "do the counting" for you. As you can see above, when the MDY function is invoked: newvar=mdy(mon_var, day_var, year_var); then, SAS will do the counting and the number you store is now the number of days since Jan 1, 1960 and then you can use a SAS date format as you envision.

 

  But, there's another challenge -- you seem to have a year and a month -- and breaking those 2 values apart is easy for a numeric variable but as you can see from the timeline and from the MDY function, SAS needs to know the DAY -- so what day would you use? 01, 15, 19??? You say you want to see the date displayed as "YYMMDD.10 Format " -- but you have not said what day value to use.

 

  In situations like this, it is very common to use 1 (since every month has a day 1) or 15 (since every month has a day 15). One easy thing to do to break apart your year and your month values (assuming that your original variable IS a numeric variable), then you can divide to get the year and the month values split out, as shown below. Note that this program will ONLY work if the original variable is a number and if the month value ALWAYS has a leading 0 in the month (201406 and NOT 20146):

dates_converted.png

 

Other methods would be needed if your original variable was a character string, not numeric, or if the months were NOT always represented as a 2 digit month. But I will leave those conditions for you to investigate in the documentation.

 

cynthia

New Contributor
Posts: 2

Re: Importing sas7bdat file

Great explanation! Thanks!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 76077 views
  • 8 likes
  • 5 in conversation