Help using Base SAS procedures

how to create multiple dataset from one dataset?

Reply
Regular Contributor
Posts: 218

how to create multiple dataset from one dataset?

Can anyone tell me what would be the code for creating multiple dataset from one dataset in one data step?

Example: I have the following info dataset

Name  City          Country

John   New York  USA

Albert Toronto      Canada

Alice  Sydney      Australia

I want to create 3 dataset called USA, Canada and Australia from info dataset above. I tried the following way, but it's not working:

data usa canada australia;

set info;

if country='usa' then output usa;

if country='Canada' then output Canada;

if country='Australia' then output Australia;

run;

can anyone help please. Thanks.

Super User
Super User
Posts: 7,039

Re: how to create multiple dataset from one dataset?

Your code should work, other than the fact that you will get zero obs in the USA data set since the value of country in your example is USA not the usa that your IF statement is testing.

Super User
Posts: 19,787

Re: how to create multiple dataset from one dataset?

Character comparison is case sensitive. So usa is not equal to USA.

If you'll have mixed case in between observations you can use upcase function.

Contributor
Posts: 70

Re: how to create multiple dataset from one dataset?

Hi,

You can try below code. it should work.

data usa canada australia;

set info;

if upcase(country)='USA' then output usa;

if upcase(country)='CANADA' then output Canada;

if upcase(country)='AUSTRALIA' then output Australia;

run;

When you use upcase function. String should be in upper case as above. whatever case in data, it should be populated in the output.

Let me know if you need any help.

Thanks,

Yaswanth J.

Super User
Posts: 7,771

Re: how to create multiple dataset from one dataset?

data usa canada australia ;

set info;

select (upcase(country));

  when ('USA') output usa;

  when ('CANADA') output canada;

  when ('AUSTRALIA') output Australia;

  otherwise;

end;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 2

Re: how to create multiple dataset from one dataset?

Hi Sir,

Please use following if statement.

If country = "USA" then output usa;

Instead of

if country = "usa then output usa;

Then you will get your desirable output.

Thanks,

Gnaneshwar,

Ask a Question
Discussion stats
  • 5 replies
  • 264 views
  • 5 likes
  • 6 in conversation