DATA Step, Macro, Functions and more

Do Until/ Do While

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Do Until/ Do While

Hi all, i'm writing a script for data step.

the raw file is like this:

(dlm=,)

ID,visit_freq,sales1,sales2,sales3,sales4

A,3,2,3,

B,4,1,,2,4

C,2,1,2

output should be like this:

ID sales

A 2

A 3

A .

B 1

B .

B 2

B 4

C 1

C 2

i have used do while but the output is a bit different:

data sales1;

     infile ' data path' dlm=',' missover;

     input ID$ visit_freq @;

     do until (visit_freq=0);

         visit_freq=visit_freq-1;

          input sales @;

          output;

     end;

     drop visit;

run;

finally i got the output is different the expected (problem of observation B)... may anyone help on this????

Thanks in advance!!


Accepted Solutions
Solution
‎07-06-2013 12:49 PM
Super User
Super User
Posts: 7,078

Re: Do Until/ Do While

You need the dsd option so that treats the adjacent delimiters (,,) as meaning a missing value.  Otherwise it treats them the same way it would treat multiple spaces in normal list mode input.

data sales1;

   infile 'data path' DSD dlm=',' TRUNCOVER;

   input ID $ visit_freq @;

   do until (visit_freq=0);

      visit_freq=visit_freq-1;

      input sales @;

      output;

   end;

   drop visit_freq;

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,078

Re: Do Until/ Do While

Looks like your data tells you how many values to read. Use an iterative loop instead.

do i=1 to visit_freq ;

  input sales @;

  output ;

end;

Occasional Contributor
Posts: 6

Re: Do Until/ Do While

Thanks Tom,

Actually it's similar to the do while/ until concept and the result would be like this?

ID sales

A 2

A 3

A .

B 1

B 2 **

B . **

B 4

C 1

C 2

i cant figure out why the rows with ** are swapped...

Solution
‎07-06-2013 12:49 PM
Super User
Super User
Posts: 7,078

Re: Do Until/ Do While

You need the dsd option so that treats the adjacent delimiters (,,) as meaning a missing value.  Otherwise it treats them the same way it would treat multiple spaces in normal list mode input.

data sales1;

   infile 'data path' DSD dlm=',' TRUNCOVER;

   input ID $ visit_freq @;

   do until (visit_freq=0);

      visit_freq=visit_freq-1;

      input sales @;

      output;

   end;

   drop visit_freq;

run;

Occasional Contributor
Posts: 6

Re: Do Until/ Do While

Thank you so much Tom!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 203 views
  • 0 likes
  • 2 in conversation