BookmarkSubscribeRSS Feed
Aquarian27
Calcite | Level 5

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

6 REPLIES 6
Haikuo
Onyx | Level 15

,

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);

data_null__
Jade | Level 19

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;
Haikuo
Onyx | Level 15

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

Haikuo

robertrao
Quartz | Level 8

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

Tom
Super User Tom
Super User

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.


sound
Calcite | Level 5

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

;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1489 views
  • 7 likes
  • 6 in conversation