BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
whymath
Barite | Level 11

I want to read a series of coordinate into SAS dataset. There are three variables: AREA, X and Y. AREA is a character variable.

When there is only one value of AREA and coresponding (X,Y) coordinates, I can use the program:

data grid;
  retain area;
  if _n_=1 then input area :$1. @;
  input x y @@;
  datalines;
E 0 150  35 155  50 550
;

The result what I want:

                               AREA    X    Y

The 1st Row:          E         0    150

The 2nd Row:        E        35   155

The 3rd Row:         E        50   550

 

But I cann't handle condition with multiple level of AREA:

data grid;
  retain area;
  if _n_=1 then input area :$1. @;
  input x y @@;
  datalines;
E 0 150  35 155  50 550
D 0 100  25 100  50 125  80 215  125 550
C 0 60   30 60   50 80   70 110  260 550
;

This program failed.

Another difficulty is there maybe 3, 4 or 5 coordinates for each level of AREA.

 

Please help, Rearrange datalines is allowed but I don't want it occupy too much row, because I am putting it into presentation.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you never having missing values of X just read until you find a missing value.

data grid;
  infile datalines truncover ;
  input area $ @ ;
  do rep=1 by 1 until(x=.);
    input x y @ ;
    if (rep=1) or (x ne .) then output;
  end;
datalines;
E 0 150  35 155  50 550
D 0 100  25 100  50 125  80 215  125 550
C 0 60   30 60   50 80   70 110  260 550
;

If you might have missing values then just read until you read past the end of the line.

data grid;
  infile datalines column=cc length=ll truncover ;
  input area $ @ ;
  do rep=1 by 1 until(cc>ll);
    input x y @ ;
    if (rep=1) or n(x,y) then output;
  end;
datalines;
E 0 150  35 155  50 550
D 0 100  25 100  50 125  80 215  125 550
C 0 60   30 60   50 80   70 110  260 550
;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

If you never having missing values of X just read until you find a missing value.

data grid;
  infile datalines truncover ;
  input area $ @ ;
  do rep=1 by 1 until(x=.);
    input x y @ ;
    if (rep=1) or (x ne .) then output;
  end;
datalines;
E 0 150  35 155  50 550
D 0 100  25 100  50 125  80 215  125 550
C 0 60   30 60   50 80   70 110  260 550
;

If you might have missing values then just read until you read past the end of the line.

data grid;
  infile datalines column=cc length=ll truncover ;
  input area $ @ ;
  do rep=1 by 1 until(cc>ll);
    input x y @ ;
    if (rep=1) or n(x,y) then output;
  end;
datalines;
E 0 150  35 155  50 550
D 0 100  25 100  50 125  80 215  125 550
C 0 60   30 60   50 80   70 110  260 550
;
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
  • 2 replies
  • 1173 views
  • 3 likes
  • 3 in conversation