BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shanti2
Calcite | Level 5

hi,

i am trying to check the values present in first 3 rows of an observation for a condition and then put rest of the values in to a new dataset.

can this be done? please help me with this.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Dataset have observations. Text files have lines.

It kind of looks like the first three lines in your file have information about the columns and the real data starts on the fourth line.

Note there is no need to attach a file to show lines of text on this forum.  Just use the Insert Code button to paste in the actual lines.  Or even easier for us to use to help you paste it as code to recreate the file.

filename csv temp;
options parmcards=csv;
parmcards4;
 , , rose,jasmine,lily,tulasi
 , ,jan,feb,mar,apr
 , ,23,19,26,30
grp1,india,100,240,140,540
grp1,burma,280,410,390,280
grp1,srilanka,190,381,305,410
grp2,india,100,240,140,540
grp2,burma,280,410,390,280
grp2,srilanka,190,381,305,410
;;;;

You can read the information on the first three lines into temporary arrays.  You can count how many columns there are while you do that.  Then read and write each value as its own observation.

 

You don't say what the numbers in the grid represent so I will just use VALUE as the variable name.

data want ;
  infile csv dsd truncover ;
  length group $5 country $20 col 8 name $30 month $3 day 8 value 8;
  array names [200] $30 _temporary_;
  array months [200] $3 _temporary_;
  array days [200] _temporary_;
  if _n_=1 then do;
     input 2*name @;
     do col=1 to dim(names) until(names[col] = ' ');
       input names[col] @;
     end;
     flowers=col-1;
     retain flowers;
     input / 2*month @;
     do col=1 to flowers;
       input months[col] @;
     end;
     input / 2*day @;
     do col=1 to flowers;
       input days[col] @;
     end;
     input;
  end;
  input group country @;
  do col=1 to flowers;
     name=names[col];
     month=months[col];
     day=days[col];
     input value @;
     output;
  end;
  input;
  drop flowers;
run;

Result

Obs    group    country     col    name       month    day    value

  1    grp1     india        1     rose        jan      23     100
  2    grp1     india        2     jasmine     feb      19     240
  3    grp1     india        3     lily        mar      26     140
  4    grp1     india        4     tulasi      apr      30     540
  5    grp1     burma        1     rose        jan      23     280
  6    grp1     burma        2     jasmine     feb      19     410
  7    grp1     burma        3     lily        mar      26     390
  8    grp1     burma        4     tulasi      apr      30     280
  9    grp1     srilanka     1     rose        jan      23     190
 10    grp1     srilanka     2     jasmine     feb      19     381
 11    grp1     srilanka     3     lily        mar      26     305
 12    grp1     srilanka     4     tulasi      apr      30     410
 13    grp2     india        1     rose        jan      23     100
 14    grp2     india        2     jasmine     feb      19     240
 15    grp2     india        3     lily        mar      26     140
 16    grp2     india        4     tulasi      apr      30     540
 17    grp2     burma        1     rose        jan      23     280
 18    grp2     burma        2     jasmine     feb      19     410
 19    grp2     burma        3     lily        mar      26     390
 20    grp2     burma        4     tulasi      apr      30     280
 21    grp2     srilanka     1     rose        jan      23     190
 22    grp2     srilanka     2     jasmine     feb      19     381
 23    grp2     srilanka     3     lily        mar      26     305
 24    grp2     srilanka     4     tulasi      apr      30     410

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Dataset have observations. Text files have lines.

It kind of looks like the first three lines in your file have information about the columns and the real data starts on the fourth line.

Note there is no need to attach a file to show lines of text on this forum.  Just use the Insert Code button to paste in the actual lines.  Or even easier for us to use to help you paste it as code to recreate the file.

filename csv temp;
options parmcards=csv;
parmcards4;
 , , rose,jasmine,lily,tulasi
 , ,jan,feb,mar,apr
 , ,23,19,26,30
grp1,india,100,240,140,540
grp1,burma,280,410,390,280
grp1,srilanka,190,381,305,410
grp2,india,100,240,140,540
grp2,burma,280,410,390,280
grp2,srilanka,190,381,305,410
;;;;

You can read the information on the first three lines into temporary arrays.  You can count how many columns there are while you do that.  Then read and write each value as its own observation.

 

You don't say what the numbers in the grid represent so I will just use VALUE as the variable name.

data want ;
  infile csv dsd truncover ;
  length group $5 country $20 col 8 name $30 month $3 day 8 value 8;
  array names [200] $30 _temporary_;
  array months [200] $3 _temporary_;
  array days [200] _temporary_;
  if _n_=1 then do;
     input 2*name @;
     do col=1 to dim(names) until(names[col] = ' ');
       input names[col] @;
     end;
     flowers=col-1;
     retain flowers;
     input / 2*month @;
     do col=1 to flowers;
       input months[col] @;
     end;
     input / 2*day @;
     do col=1 to flowers;
       input days[col] @;
     end;
     input;
  end;
  input group country @;
  do col=1 to flowers;
     name=names[col];
     month=months[col];
     day=days[col];
     input value @;
     output;
  end;
  input;
  drop flowers;
run;

Result

Obs    group    country     col    name       month    day    value

  1    grp1     india        1     rose        jan      23     100
  2    grp1     india        2     jasmine     feb      19     240
  3    grp1     india        3     lily        mar      26     140
  4    grp1     india        4     tulasi      apr      30     540
  5    grp1     burma        1     rose        jan      23     280
  6    grp1     burma        2     jasmine     feb      19     410
  7    grp1     burma        3     lily        mar      26     390
  8    grp1     burma        4     tulasi      apr      30     280
  9    grp1     srilanka     1     rose        jan      23     190
 10    grp1     srilanka     2     jasmine     feb      19     381
 11    grp1     srilanka     3     lily        mar      26     305
 12    grp1     srilanka     4     tulasi      apr      30     410
 13    grp2     india        1     rose        jan      23     100
 14    grp2     india        2     jasmine     feb      19     240
 15    grp2     india        3     lily        mar      26     140
 16    grp2     india        4     tulasi      apr      30     540
 17    grp2     burma        1     rose        jan      23     280
 18    grp2     burma        2     jasmine     feb      19     410
 19    grp2     burma        3     lily        mar      26     390
 20    grp2     burma        4     tulasi      apr      30     280
 21    grp2     srilanka     1     rose        jan      23     190
 22    grp2     srilanka     2     jasmine     feb      19     381
 23    grp2     srilanka     3     lily        mar      26     305
 24    grp2     srilanka     4     tulasi      apr      30     410

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 938 views
  • 3 likes
  • 2 in conversation