Help using Base SAS procedures

Ampersand (&) modifier doesn't work

Reply
Occasional Contributor
Posts: 9

Ampersand (&) modifier doesn't work

Data Test;

input name & $9. Sale ;

datalines;

Ron Bill 435

Paul Anka 657

;

Run;

In the above example, & modifier does not work. Might be because It does not encounter two consecutive blank after name variable. In that case, what other method can be used to read this data ?

Arun

Respected Advisor
Posts: 3,156

Re: Ampersand (&) modifier doesn't work

Posted in reply to Aquarian27

,

if your data will come as is (no other issues, such as middle name, etc), you can try to use _infile_; Otherwise, you will need to involve more complex logic or functions.

Data Test;

input;

length name $9.;

name=catx(' ',scan(_infile_,1),scan(_infile_,2));

Sale=input(scan(_infile_,3),best.);

datalines;

Ron Bill 435

Paul Anka 657

;

Run;

Haikuo

UPdate: on my second thought, you can just do: input (firstname lastname) (:$8.) Sale;

then :

name=catx(' ',firstname,lastname);

Respected Advisor
Posts: 3,799

Re: Ampersand (&) modifier doesn't work

You could try sneeking up on it.  Then you don't have to now how many words in name.

Data Test;
   input @;
   call scan(_infile_,-1,_p,_l);
   _l=_p-2;
  
input name $varying32. _l sale;
   drop _:;
   datalines;
Ron Bill 435
Paul Anka 657
;
  
Run;
Respected Advisor
Posts: 3,156

Re: Ampersand (&) modifier doesn't work

Posted in reply to data_null__

Awesome!. Get to learn $varingw. informat. Thanks, Data_null_!

Haikuo

Super Contributor
Posts: 1,041

Re: Ampersand (&) modifier doesn't work

Hai,

I did not understand what Data null,s code is doing... Could you please explain what these lines are doing.

input @;
   call scan(_infile_,-1,_p,_l);
   _l=_p-2;
  
input name $varying32. _l sale;
   drop _:;
   datalines;

Regards

Super User
Super User
Posts: 7,054

Re: Ampersand (&) modifier doesn't work

Posted in reply to robertrao

input @;

Begin reading the next line. This will set the automatic variable _INFILE_ with the content of the next line.


   call scan(_infile_,-1,_p,_l);

Find the length and position of a word in a text string.  _INFILE_ is the line of text, _P and _L will get the position and length. Because -1 is negative it scans from the end to the front. So -1 is the last word.


   _l=_p-2;

Set _L to two positions before the beginning of the last word. This will be used in the next statement.


   input name $varying32. _l sale;

Read the first _L characters of the input line into variable NAME and then read SALE from the rest of the line.


   drop _:;

Do not keep the _L and _P variables.  The colon is a wildcard so it matches any variable name that starts with an underscore.


   datalines;

This marks the beginning of the in line data.  Us old timers use CARDS; instead.  If you will be reading from a file instead then remove this and insert an INFILE statement before the first INPUT statement.


N/A
Posts: 1

Re: Ampersand (&) modifier doesn't work

Posted in reply to Aquarian27

Hello Arun,

U need to be use code like this to run .

Data Test,

input name & $9. sale;

dataline;

Ron Bill   435

Paul Anka   657

;


Ask a Question
Discussion stats
  • 6 replies
  • 362 views
  • 7 likes
  • 6 in conversation