BookmarkSubscribeRSS Feed
Anurag_Kumar
Calcite | Level 5

I have a dataset with a variable animal type(example). There are 30 animals overall and any row might have any number of animals:

 

Big cat; Tiny strong dog; White pony

Big cat;

White pony; Small rabbit;

Tiny strong dog; White pony

 

I need to separate these into as many columns as required for a particular row. There is no restriction of keeping one column for one animal type. I am using the below code but there are multiple rows being generated along with multiple columns and these columns are empty.

 

data want;
set have;

array var(30);

do i=1 to 30;
var(i)=scan(Animal,i,";");
end;

run;

 

Please advise.

 

SAS EG version: 7.15

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Remove OUTPUT; from your code.

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @Anurag_Kumar  Welcome to SAS communities as I see your tag as New contributor. Can you please post a sample of the OUTPUT you want for the input? Thanks!

PaigeMiller
Diamond | Level 26

Perhaps you have read the data into your SAS data set improperly? 

 

Also, are there ERRORs, WARNINGs or NOTEs in your SAS log? Please show us the ENTIRE SAS log.

At this point, we need the SAS code that reads this data, the LOG, and you also need to show us the desired output.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Okay, I can get this to work, does this meet your needs?

 

data have;
    input animal &$200.;
    cards4;
    Big cat; Tiny strong dog; White pony
    Big cat; guinea pig
    White pony; Small rabbit;
    Tiny strong dog; White pony
    ;;;;
run; 

data want;
    set have;
    array var(30) $ 40;
    do i=1 to 30;
        var(i)=scan(animal,i,";");
    end;
run;
--
Paige Miller
ballardw
Super User

@Anurag_Kumar wrote:

I have a dataset with a variable animal type(example). There are 30 animals overall and any row might have any number of animals:

 

Big cat; Tiny strong dog; White pony

Big cat;

White pony; Small rabbit;

Tiny strong dog; White pony

 

I need to separate these into as many columns as required for a particular row. There is no restriction of keeping one column for one animal type. I am using the below code but there are multiple rows being generated along with multiple columns and these columns are empty.

 

data want;
set have;

array var(30);  <= this defines an array of numeric values so of course a number cannot be "Big cat"

do i=1 to 30;
var(i)=scan(Animal,i,";");
end;

run;

 

Please advise.

 

SAS EG version: 7.15


Array var(30) $ 25; to create an array of character values able to hold up  to 25 characters per value. Set the length as seems appropriate.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 5422 views
  • 0 likes
  • 4 in conversation