BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hoibai
Obsidian | Level 7

how can I separate a csv infile with this format into a outfile in different columns.

Infile: AD:1/1234 AT:22/234567 .....;

Outfile with columns:

AREANUMBERVALUE
AD11234
AT22234567
etcetcetc
etcetcetc

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Hoibai
Obsidian | Level 7

I did that, but the file is empty???

data want;
infile '/data/products/Table/Q3_2023/checks_202307.csv'
DLM = ": /" firstobs=2; /* use multiple delimiters */
*modify to specify the path to your CSV file and change firstobs to indicate the row that the observation is found;
input AREA :$2. NUMBER :best32. VALUE :commax32. @@; /* <- stay in the same line for more data */
format VALUE best32.;

;;;;
run;
proc print data=want;
run;

Hoibai_0-1707370391157.png

 

View solution in original post

20 REPLIES 20
yabwon
Onyx | Level 15

Like that:

data want;
  infile CARDS4 DLM = ":/"; /* use multiple delimiters */
  input AREA $ 2. NUMBER VALUE;
cards4;
AD:1/1234 
AT:22/234567
;;;;
run;
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Hoibai
Obsidian | Level 7

my file look like this:

Hoibai_0-1707310818323.png

 

yabwon
Onyx | Level 15

Paste it as text, I'm not going to retype it myself...

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



mkeintz
PROC Star

@Hoibai said:

 

my file look like this:

mkeintz_0-1707312826813.png

 

Please put your data in the form of a text block (use the  </> icon), instead of an image.  Most of us are not going to transcribe the image of characters you provided.

 

And given that the layout of the additional line of data you provided is different, show how it fits your AREA/NUMBER/VALUE data structure.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Hoibai
Obsidian | Level 7

the read file looks like this:

AD:1/1234,20 AT:2/23456,35 AU:3/34567,59 CA:4/56789,05 CH:5/54321,55;

 

the output file should be like this:

Columms:

Area    number     value

AD       1                  1234,20

AT       2                  23456,35

AU      3                  34567,59

CA      4                  56789,05

CH      5                  54321,55

yabwon
Onyx | Level 15
data want;
  infile CARDS4 DLM = ": /"; /* use multiple delimiters */
  input AREA :$2. NUMBER :best32. VALUE :commax32. @@; /* <- stay in the same line for more data */
  format VALUE best32.;
cards4;
AD:1/1234 AT:22/2345,67 XX:17/123,456
ZZ:1/1234 TT:22/2345,67 FU:17/123,456
;;;;
run;
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

Looks good, but you don't need the BEST32.  First of all using BEST as the name of an INFORMAT is just silly.  BEST is the name of a FORMAT.  It is used to find the "best" way to display a number in a limited number of characters.  The concept of a "BEST" way to convert a string into a number makes no sense.  There is only one way to store a number.   If you use BEST as an informat SAS will just use the normal numeric informat anyway so why bother?  And I see no reason to attach the BEST32. format to the values in the example text.  Just let SAS use its default method to display the values.

 

And there is no need to add a width to a numeric informat when using LIST mode (which is what the colon modifier is doing).  In LIST MODE the width on the informat is ignored, the whole next "word" in the line is read however long it is.  The width on the $ informat does something useful.  It gives the compiler the information it needs to GUESS that you wanted the variable AREA to be define as length $2 instead of the default of $8.

 

  input AREA :$2. NUMBER VALUE :commax. @@; 
Hoibai
Obsidian | Level 7

How do i can import this CSV File into sas? Thanks.

Kurt_Bremser
Super User

@Hoibai wrote:

How do i can import this CSV File into sas? Thanks.


That's exactly what @yabwon's code does. It reads the data into the dataset WANT in library WORK.

To read from a file instead of inline, use the filename in the INFILE statement in place of CARDS, and omit the CARDS4 block.

Patrick
Opal | Level 21

@Hoibai wrote:

How do i can import this CSV File into sas? Thanks.


@yabwon provided already the logic for this. If the first row contains header information then also use in the infile statement firstobs=2

 

If you want us to share tested code then the minimum you need to provide is representative sample data in usable form. Either posted via the "insert code" icon or as an attachment.

Reeza
Super User
data want;
  infile '/home/reeza/myfile.csv' DLM = ": /" firstobs=2; /* use multiple delimiters */
*modify to specify the path to your CSV file and change firstobs to indicate the row that the observation is found;
  input AREA :$2. NUMBER :best32. VALUE :commax32. @@; /* <- stay in the same line for more data */
  format VALUE best32.;

;;;;
run;
proc print data=want;
run;

@Hoibai wrote:

How do i can import this CSV File into sas? Thanks.


 

Hoibai
Obsidian | Level 7

I did that, but the file is empty???

data want;
infile '/data/products/Table/Q3_2023/checks_202307.csv'
DLM = ": /" firstobs=2; /* use multiple delimiters */
*modify to specify the path to your CSV file and change firstobs to indicate the row that the observation is found;
input AREA :$2. NUMBER :best32. VALUE :commax32. @@; /* <- stay in the same line for more data */
format VALUE best32.;

;;;;
run;
proc print data=want;
run;

Hoibai_0-1707370391157.png

 

Tom
Super User Tom
Super User

Assuming you did not get an ERROR that the file could not be found then it means that either the file is EMPTY or it has only ONE line.

 

LOOK at the file and see what it actually has.

 

Best way is to LOOK at it using a SAS data step, since your goal is to ultimately use SAS to read the file.

 

This step will print the first 3 lines of your file to the SAS log.

data _null_;
  infile '/data/products/Table/Q3_2023/checks_202307.csv' obs=3;
  input;
  list;
run;

How many lines did it find?

How long were they?

Did the output from the LIST statement include the two lines of hexcodes underneath any parts of the lines?  That will happen when SAS sees an unprintable character code.  Such as LF character ('0A'x) or CR character ('0D'x).

 

One common problem is that the file does NOT contain lines.  In which case your little program to look at the file will find only ONE line.

 

If it is using fixed length records, or it is just a binary file, try using the RECFM=N on the INFILE statement.  But you will probably then need to remove the trailing @@ from the INPUT statement to prevent an infinite loop.

 

Another issue that sometimes comes up is that the file does have lines, but instead of the normal end of line character of LF (used by Unix) or CR and LF (used by Windows, aka DOS) the file is using just CR as the end of line character.  That is what the original MacOS from the 1980's used before they converted MacOS to be a flavor of Unix.  In that case add TERMSTR=CR to the INFILE statement instead.

Hoibai
Obsidian | Level 7

I have another question.

my input file looks like this:

AD:1/1234,20 AT:2/23456,35 AU:3/34567,59 CA:4/56789,05 CH:5/54321,55; DE:3/34567,59 ES:12/2341,25;

If there is a semicolon in the line, then the data up to the next semicolon should be output to another file. Is this visible when reading the CSV file?

Example:

AD:1/1234,20 AT:2/23456,35 AU:3/34567,59 CA:4/56789,05 CH:5/54321,55

Output want;

DE:3/34567,59 ES:12/2341,25

Output want_1

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 20 replies
  • 1018 views
  • 8 likes
  • 7 in conversation