BookmarkSubscribeRSS Feed
mlogan
Lapis Lazuli | Level 10

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.

5 REPLIES 5
Tom
Super User Tom
Super User

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.

Reeza
Super User

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.

yaswanthj
Fluorite | Level 6

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.

Kurt_Bremser
Super User

data usa canada australia ;

set info;

select (upcase(country));

  when ('USA') output usa;

  when ('CANADA') output canada;

  when ('AUSTRALIA') output Australia;

  otherwise;

end;

run;

GnaneshwarChaudhari
Calcite | Level 5

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,

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1751 views
  • 5 likes
  • 6 in conversation