BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I have a question please.

I will try to simplify it by a simple example.

There is an external table that I receive.

In this table for example there are 3 fields and should be for example 4 rows.

For example:The data should be like that:

  a   b   c

  d   e   f

  g   h   I

  J   k   L

But I get it like that:

 abc  de  f

 g     h     I

 j      K     L

I want to ask If there is a way to fix the data in a code.

For example: I want to have in each row 3 fields only and each field will contain 1 words(string).

Best solution will be to have a flexible program that can fix data for any number of fields.

thanks

Roms

 

4 REPLIES 4
LinusH
Tourmaline | Level 20
Well, anything is possible, especially when it comes to SAS.
But if you don't have a specific (limited) no of cases you wish to handle, chances are that you will be in a never ending loop of fixing new data quality issues, and the result will an unmanageable piece of code.
So, first challenge your source to give you data with a sustainable structure.
Data never sleeps
Ronein
Meteorite | Level 14

Thanks for your reply.

I gave a specific example.

Do you know how to repair it in sas code?

This is my question.

 

Kurt_Bremser
Super User

Make an array with 3 variables.

Retain a counter, initialize to 1.

Do an input without a variable (input line ends up in _infile_)

Compress the _infile_ to remove blanks.

Do for all characters in _infile_:

  Assign the current character to the current array member

  Increment the counter

  If the counter is > 3, reset to 1 and output

When the end of file is reached, output if counter is not 1 (incomplete last observation)

 

How to put that pseudocode into data step code is left as an exercise for the student 😉

 

 

 

Patrick
Opal | Level 21

@Ronein

The challenge starts with your specification.

You define "...and each field will contain 1 words(string)." but then you show us as very first word "abc" but expect this single word to get split up into 3 different variables. What should it be? 1 non-blank character per variable OR words?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 790 views
  • 2 likes
  • 4 in conversation