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
Calcite | Level 5

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,

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!

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.

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