BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

data want ;
input name $15. age ;
cards ;
happy new year 22
advance 23
;

if we run program it gives first record but it doesn't give second record 

8 REPLIES 8
Kurt_Bremser
Super User

If you have blanks in data items, use a delimiter that is not a blank, or you will have to resort to unnecessarily complicated programming:

data want;
infile cards dlm=',';
input name :$15. age;
cards;
happy new year,22
advance,23
;
run;
thanikondharish
Calcite | Level 5
without using delimiter=','
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Delimiters are not optional.  There needs to be a logical way of identifying where one item starts and finishes.  In your example this is not possible.  Therefore, due to this daft data you need to read it all in one big string, then write code to post process it into your required format.  Simply put wrong data format means more work for you.

data want;
  length str $200;
  infile datalines dlm="-";
  input str $;
  age=scan(str,-1," ");
  name=substr(str,1,findc(strip(str)," ","b"));
datalines;
happy new year 22
advance 23
;
run;

Me, I would be sending this back with all due haste, or putting my required resource hours up considerably.

Do note, I use delimiter="-" in the above code to essentially tell the system not to use blanks.

Kurt_Bremser
Super User

Don't be an idiot and make your life hard for no reason. Inserting a suitable delimiter is child's play compared to the hoops you have to jump through otherwise.

eg if you positively know you have only two items and that the second will NEVER contain a blank, you can do this:

data want;
infile cards truncover;
input line $80.;
name = substr(line,1,length(line) - length(scan(line,-1)));
age = input(scan(line,-1),best.);
drop line;
cards;
happy new year 22
advance 23
;
run;

But if there are other columns that contain blanks, it gets progressively harder.

 

Up to the point where you go to the one who sent you this with a shotgun in your hand and shoot her/him with a crazy smile on your face. After which you are taken away by some muscular guys with friendly faces in white suits and a jacket that closes in the back.

ballardw
Super User

@thanikondharish wrote:
without using delimiter=','

Why not? If you don't like commas for a delimiter pick something else like | or * or #.

 

If you are reading data that is in fixed column format such that a value always ends in column 15, then this will be possible just specify the columns in the input statement.. But if you are dealing with data where the start and end positions change then you will either have to 1) add a delimiter of some sort or 2) read the entire data line as one string variable and then parse it.

 

thanikondharish
Calcite | Level 5
it means without using delimiters
error_prone
Barite | Level 11

I could repeat what @RW9 and @Kurt_Bremser already said: data without delimiters is nothing more but useless crap. In your example, you could use the code already posted to get the data, but as soon as the data is more complex, maintaining the code required will become time consuming, hard work.

ballardw
Super User

@thanikondharish wrote:
it means without using delimiters

42

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
  • 8 replies
  • 1389 views
  • 5 likes
  • 5 in conversation