BookmarkSubscribeRSS Feed
pawandh
Fluorite | Level 6

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?

 

3 REPLIES 3
Reeza
Super User
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.
stat_sas
Ammonite | Level 13

data t1;

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

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 479 views
  • 1 like
  • 4 in conversation