sas programming

Reply
Contributor
Posts: 59

sas programming

data t;
input sno category$ 20.;
cards;
1 dairy floor eggs
2 floor milk
3 eggs bread
4 bread butter butter
;run;

 

i have this data set and i want distinct category (dairy,floor,eggs,milk,bread,butter).

I can do like this


data t1;
set t;
do i=1 to 3;
x=scan(category,i);
output;
end;
run;
proc print;run;

but here i dnt knw how many times i have to run the loop like 3 or 4 or 5 times.Any optimum way?

 

Super User
Posts: 17,819

Re: sas programming

Countw() will count number of words in a variable. Use that to create a variable that holds the number of iterations and use it in your do loop.
Trusted Advisor
Posts: 1,204

Re: sas programming

data t1;

set t;
do i=1 to countw(category);
x=scan(category,i);
output;
end;
run;

Respected Advisor
Posts: 4,646

Re: sas programming

Read the categories one by one until the end of each line:

 

data t;
infile datalines truncover;
input sno category :$20. @;
do until(missing(category));
    output;
    input category :$20. @;
    end;
datalines;
1 dairy floor eggs
2 floor milk
3 eggs bread
4 bread butter butter
;
PG
Ask a Question
Discussion stats
  • 3 replies
  • 289 views
  • 1 like
  • 4 in conversation