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

Dear community,

               I am trying to import this short data and I am unsuccessful with this code. Can anyone help me to import the data either proc import or any other way? 


proc import datafile="C:\newfolder\class.csv"
out=check
dbms=csv
replace;
getnames=yes;
namerow=4;
datarow=5;
run;

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The csv file looks something like this (making allowances that the Forum makes CSV Excel for download )

 

Report run date and time : 10FEB14 00:00,,,,
,,,,
,,,,
name,sex,age,height,weight
Alfred ,M ,14,69,112.5
Alice ,F ,13,56.5,84
Barbara ,F ,13,65.3,98
Carol ,F ,14,62.8,102.5
Henry ,M ,14,63.5,102.5
James ,M ,12,57.3,83
Jane ,F ,12,59.8,84.5
Janet ,F ,15,62.5,112.5
Jeffrey ,M ,13,62.5,84
John ,M ,12,59,99.5
Joyce ,F ,11,51.3,50.5
Judy ,F ,14,64.3,90
Louise ,F ,12,56.3,77
Mary ,F ,15,66.5,112
Philip ,M ,16,72,150
Robert ,M ,12,64.8,128
Ronald ,M ,15,67,133
Thomas ,M ,11,57.5,85
William ,M ,15,66.5,112

So the appropriate solution might be

1) use proc import with the correct DATAROW option to read the body of the data

2) copy the generated text of data step used to read the body of the data

3) Edit the infile statement generated NOT to skip to row 5 but instead

4) write a parsing statement to execute a read of the first line to find that datestamp, keep it

And then skip to reading the remaining data

 

data example;
   infile datalines dlm=',';
   informat datestamp anydtdtm20.;
   format datestamp datetime20.;
   retain datestamp;
   informat name $10. sex $1. age height weight f5.;
   
   input @;
   if _infile_ =: "Report run date and time" then
      input @28 datestamp
      /
      /
      /
      ;

   else do;
     input name sex age heigh weight;
     output;
   end;
datalines;
Report run date and time : 10FEB14 00:00,,,,
,,,,
,,,,
name,sex,age,height,weight
Alfred ,M ,14,69,112.5
Alice ,F ,13,56.5,84
Barbara ,F ,13,65.3,98
Carol ,F ,14,62.8,102.5
Henry ,M ,14,63.5,102.5
James ,M ,12,57.3,83
Jane ,F ,12,59.8,84.5
Janet ,F ,15,62.5,112.5
Jeffrey ,M ,13,62.5,84
John ,M ,12,59,99.5
Joyce ,F ,11,51.3,50.5
Judy ,F ,14,64.3,90
Louise ,F ,12,56.3,77
Mary ,F ,15,66.5,112
Philip ,M ,16,72,150
Robert ,M ,12,64.8,128
Ronald ,M ,15,67,133
Thomas ,M ,11,57.5,85
William ,M ,15,66.5,112
;

View solution in original post

9 REPLIES 9
buddha_d
Pyrite | Level 9

Is there a way to get the date stamp from the file? 

Thanks

Shmuel
Garnet | Level 18

What do you mean by "I am unsuccessful with this code" ?

Are there error or warning messages in the log?

Please post your log using the {i} icon.

buddha_d
Pyrite | Level 9

Shmuel, 

             sorry for not posting the log. please check the attachment

 

thanks 

 

 

Kurt_Bremser
Super User

Please do NOT post pictures of logs embedded in Word documents. Just copy/paste the log into a window opened with the {i} button.

I suggest you follow Maxim 1 and study the documentation for proc import. There is no namerow statement.

buddha_d
Pyrite | Level 9

Thanks Kurt. I checked the link and you are right. I am posting the log now. I could get the data imported using infile statement. But, could you point me anyway, I could get the date stamp from the file?? 

1
2    proc import datafile="C:\Daya\class.csv"
3                out=check
4                dbms=csv
5                replace;
NOTE: The previous statement has been deleted.
NOTE: The previous statement has been deleted.
6                getnames=yes;
7                namerow=4;
                 -------
                 180
ERROR 180-322: Statement is not valid or it is used out of proper order.

8                datarow=5;
9    run;


NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IMPORT used (Total process time):
      real time           0.06 seconds
      cpu time            0.03 seconds


Shmuel
Garnet | Level 18

According to documentation there is no NAMEROW statement.

I have not used mutch the proc import but you may try:

Cancel the namerow in your code.

May be the procedure read the names from datarow - 1.

 

Kurt_Bremser
Super User

A csv file has, by definition, the column names in the first row.

If that is not the case, you will need to write your own data step where you manually set the column names and attributes and skip the first four lines with a subsetting if.

ballardw
Super User

The csv file looks something like this (making allowances that the Forum makes CSV Excel for download )

 

Report run date and time : 10FEB14 00:00,,,,
,,,,
,,,,
name,sex,age,height,weight
Alfred ,M ,14,69,112.5
Alice ,F ,13,56.5,84
Barbara ,F ,13,65.3,98
Carol ,F ,14,62.8,102.5
Henry ,M ,14,63.5,102.5
James ,M ,12,57.3,83
Jane ,F ,12,59.8,84.5
Janet ,F ,15,62.5,112.5
Jeffrey ,M ,13,62.5,84
John ,M ,12,59,99.5
Joyce ,F ,11,51.3,50.5
Judy ,F ,14,64.3,90
Louise ,F ,12,56.3,77
Mary ,F ,15,66.5,112
Philip ,M ,16,72,150
Robert ,M ,12,64.8,128
Ronald ,M ,15,67,133
Thomas ,M ,11,57.5,85
William ,M ,15,66.5,112

So the appropriate solution might be

1) use proc import with the correct DATAROW option to read the body of the data

2) copy the generated text of data step used to read the body of the data

3) Edit the infile statement generated NOT to skip to row 5 but instead

4) write a parsing statement to execute a read of the first line to find that datestamp, keep it

And then skip to reading the remaining data

 

data example;
   infile datalines dlm=',';
   informat datestamp anydtdtm20.;
   format datestamp datetime20.;
   retain datestamp;
   informat name $10. sex $1. age height weight f5.;
   
   input @;
   if _infile_ =: "Report run date and time" then
      input @28 datestamp
      /
      /
      /
      ;

   else do;
     input name sex age heigh weight;
     output;
   end;
datalines;
Report run date and time : 10FEB14 00:00,,,,
,,,,
,,,,
name,sex,age,height,weight
Alfred ,M ,14,69,112.5
Alice ,F ,13,56.5,84
Barbara ,F ,13,65.3,98
Carol ,F ,14,62.8,102.5
Henry ,M ,14,63.5,102.5
James ,M ,12,57.3,83
Jane ,F ,12,59.8,84.5
Janet ,F ,15,62.5,112.5
Jeffrey ,M ,13,62.5,84
John ,M ,12,59,99.5
Joyce ,F ,11,51.3,50.5
Judy ,F ,14,64.3,90
Louise ,F ,12,56.3,77
Mary ,F ,15,66.5,112
Philip ,M ,16,72,150
Robert ,M ,12,64.8,128
Ronald ,M ,15,67,133
Thomas ,M ,11,57.5,85
William ,M ,15,66.5,112
;
buddha_d
Pyrite | Level 9

Thanks ballardw. It works well now. 

 

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