BookmarkSubscribeRSS Feed
noobs
Calcite | Level 5

Hello,

I am using non-sequential #n line pointer control to read following data, where one observation needs to be created out of three records:

ROMA TOMATO

FRESHPOINT CA

1.80 DAILY

GREEN BEANS

ORGANICS NC

1.20 WEEKLY


Sequence of variables stored in data set has to be as follows: 

  1. Supplier (FRESHPOINT)
  2. State (CA)
  3. Rate (1.8)
  4. Frequency (DAILY)
  5. VName (ROMA TOMATO)


data produce.vegetables;

     infile vegdata;

     input #2 Supplier $ State $

           #3 Rate 4. Frequency $

           #1 VName;

run;


However, how do I change sequence of variables to


  1. Supplier
  2. Rate
  3. State
  4. VName
  5. Frequency


Thanks much,

Dhanashree

4 REPLIES 4
ballardw
Super User

If you are concerned about the order variables appear in the dataset for your purpose the easiest would be to place format statements before the input.

Format Supplier $25. Rate 4.0 State $15. Vname $15. Frequency $10.;

Input ....

noobs
Calcite | Level 5

Ok, using FORMAT statement is good practice and I will use it, however will it resolve issue of reading State variable correctly because it is preceded by Supplier variable which is not fixed length, hence one cannot use column pointer @?

format Supplier $25. Rate 3.1 State $2. VName $15. Frequency $10.;

input #2 Supplier

         #3 Rate

         #2 State

         #1 VName

         #3 @6 Frequency;

Reading Frequency with @6 is possible as Rate will always be fixed width 3 with n.n format.

However in INPUT statement, when using #2 State will it not create problem as pointer may not be placed at right column always for every record? Is there any other solution?

Tom
Super User Tom
Super User

FORMAT statements do NOT define the variables other than in the same way that SAS will guess what type of variable to define based on how it is first used.  To define the variable you will get better results using LENGTH or ATTRIB statement to clearly define them.

Tom
Super User Tom
Super User

Define the variables before the INPUT statement.  Probably a good practice anyway.

data produce.vegetables;

  length supplier $20 state $2 rate 8 vname $50 frequency $12 ;

  infile vegdata truncover ;

  input vname $50.   /* Useful to use a format to allow for spaces in the names */

      / Supplier State

      / Rate  Frequency

  ;

run;

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1555 views
  • 0 likes
  • 3 in conversation