Solved
Contributor
Posts: 71

# Data Set Help.

{Code}

data demo;

input stno name & :\$21. age color :\$;

cards;

100 kate john 30 brown

101 Blake Lynch 29 brown

;

run;

proc print data=demo;

run;

{Code}

{output}

Obs    stno           name           age    color

1      100    kate john 30 brown    101    Blake

But my expected output is

Obs    stno           name           age    color

1       100            kate john      30      brown

2       101            Blake Lynch 29      brown.

Can you let me where Am i doing wrong. Even I place & im not getting.

Version:sas base 9.1

regards,

vnky

Accepted Solutions
Solution
‎08-15-2013 03:26 AM
Contributor
Posts: 45

## Re: Data Set Help.

Hi Vnky,

If you we can be sure that we are only inputting two names (e.g. no middle names or initials) then we can input each name seperately and concatenate them.

data demo;

input stno name1 \$ name2 \$ age color :\$;

cards;

100 kate john 30 brown

101 Blake Lynch 29 brown

;

data demo2(drop=name1 name2);
stno=.;
length name \$21.;
set demo;
name=trim(name1)||" "||trim(name2);
run;

proc print data=demo2;

run;

This gives the exact output.

If we would like to have some names with one word or three words we would need to use delimiters in your raw data file.

Regards,

-Murray.

All Replies
Solution
‎08-15-2013 03:26 AM
Contributor
Posts: 45

## Re: Data Set Help.

Hi Vnky,

If you we can be sure that we are only inputting two names (e.g. no middle names or initials) then we can input each name seperately and concatenate them.

data demo;

input stno name1 \$ name2 \$ age color :\$;

cards;

100 kate john 30 brown

101 Blake Lynch 29 brown

;

data demo2(drop=name1 name2);
stno=.;
length name \$21.;
set demo;
name=trim(name1)||" "||trim(name2);
run;

proc print data=demo2;

run;

This gives the exact output.

If we would like to have some names with one word or three words we would need to use delimiters in your raw data file.

Regards,

-Murray.

Posts: 1,147

## Re: Data Set Help.

data demo;

input stno name \$11.  age  colour \$ ;

name=compress(name,'0123456789');

age=input(compress(substr(_infile_,15,18),,'kd'),2.);

cards;

100 kate john 30 brown

101 Blake Lynch 29 brown

;

run;

proc print data=demo;

run;

Thanks,

Thanks,
Jag
Super Contributor
Posts: 644

## Re: Data Set Help.

Your solution will work for the examples cited but not if any subsequent name is longer than a few characters (eg Jagadish) or more than 2 words.  This could be a candidate for a regular expression solution; my approach would be to extract the stno, age and color info and then remove them from the input string, leaving the name.  You can't use tranwrd() or transtr() to remove the color because some colors are also names.

data demo;

infile cards ;

input    stno  ;

length  stno 8

name \$21

age  8

color \$8

;

color = scan (_infile_, -1) ;               /* last word in input */

age = input (scan (_infile_, -2), 3.) ;  /* next last word */

temp = compress (_infile_,,"KAS") ;    /* squeeze out the numerals */

name = left (substr (temp, 1, (length(temp) - length(color))) ;

drop temp ;

cards;

100 kate john 30 brown

101 Blake Lynch 29 brown

;

/* untested code */

Richard

Message was edited by: Richard Carson Updated the keep string in compress to include spaces ["KAS" => "keep alpha characters and spaces"]

Super Contributor
Posts: 644

## Re: Data Set Help.

The reason why the original code does not work is because the & input modifier expects to find a double space as the terminator for the value of the variable.  Since the compress function I suggested would remove the age but leave the following space the following code might work:

data demo;

infile cards ;

input    stno  @ ;

length  stno 8

name \$21

age  8

color \$8

;

color = scan (_infile_, -1) ;               /* last word in input */

age = input (scan (_infile_, -2), 3.) ;  /* next last word */

_infile_ = compress (_infile_,,"KAS") ;    /* squeeze out the numerals */

input @1 name & ;

cards;

100 kate john 30 brown

101 Blake Lynch 29 brown

;

/* untested code */

Richard

🔒 This topic is solved and locked.