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 is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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 is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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: 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
  • 1031 views
  • 1 like
  • 4 in conversation