BookmarkSubscribeRSS Feed
stataq
Quartz | Level 8

Hello,

 

I would like to build new data `all`, with 10 ds: ds_1 to ds_10. however ds_5 is an empty dataset. what should I do?

 

currently I have following codes but it wont give me the ds I want:

```

data all;

set ds_:;

run;

```

 

Is it a  smart way to build all without picking out the empty ds_*?

 

Thanks.

6 REPLIES 6
Quentin
Super User

An empty dataset, meaning 0 obs, should not be a problem.  Below test code runs fine:

 

data ds_1 ds_2(where=(0)) ds_3 ;
  do i=1 to 5 ;
    output ;
  end ;
run ;

data all ;
  set ds_: ;
run ;

Can you share a small example of your problem?

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
PaigeMiller
Diamond | Level 26

@stataq wrote:

Hello,

 

I would like to build new data `all`, with 10 ds: ds_1 to ds_10. however ds_5 is an empty dataset. what should I do?

 

currently I have following codes but it wont give me the ds I want:

```

data all;

set ds_:;

run;

```

 

Is it a  smart way to build all without picking out the empty ds_*?

 

Thanks.


Explain what happens when you try this. Do you get an error? If so, show us the log. Do you get the wrong result? If so, explain what is wrong.

 

When I try this, it works fine. No errors in the log. Expected output.

 

/* Create fake data */
data ds_1 ds_2;
    set sashelp.class;
    if name='Jane' then output ds_1;
    else if name='Robert' then output ds_2;
run;
data ds_3;
    delete;
run;

data all;
    set ds_:;
run;

 

--
Paige Miller
stataq
Quartz | Level 8

When I look further, I found 1 var in datasets has been defined as both character and numeric. Is it a quick way to fix this? 

 

For example: var `test1` should be a character var, but when it is all empty, ds_* put it as numeric. How can I update  `test1` in all ds_* to character?

ballardw
Super User

@stataq wrote:

When I look further, I found 1 var in datasets has been defined as both character and numeric. Is it a quick way to fix this? 

 

For example: var `test1` should be a character var, but when it is all empty, ds_* put it as numeric. How can I update  `test1` in all ds_* to character?


You really have to fix the process that is creating your data sets, even your empty data sets

Show the code that makes that empty data set.

And may require going back multiple steps in your process.

 

The reason to fix the process is moderately simple. I could "fix" one data set for one variable pretty easily. Suppose the next time around you have 5 data sets with the same variable character and 5 with the variable numeric. Which should be which? Fixing 5 sets gets to be more work. And if you start having this issue with multiple variables the work increases exponentially.  Fix the process one time and you don't have to deal with it again.

 

Of course this begs the whole "empty dataset" question which is a similar kettle of fish.

Quentin
Super User

Agree with @ballardw , there's isn't a quick fix for a variable type mismatch.  Best would be to go back and solve why empty datasets have variables with the wrong type.

 

If you don't want to do that, messier hack would be to add a step to check all of your ds_* datasets and delete any of them that have 0 obs, before you use SET ds_: ; to concatenate them.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
ballardw
Super User

Show us the LOG resulting from that code.

Then describe what is not present that you want. Or what is present that you don't want.

 

I'm afraid that "give me the ds I want" requires mindreading to know what you want which is notoriously unreliable over the internet.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 890 views
  • 1 like
  • 4 in conversation