DATA Step, Macro, Functions and more

How to copy only selected variables from a Null dataset to another dataset?

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

How to copy only selected variables from a Null dataset to another dataset?

Hi Guys,

I have a null dataset with 20 variables. i want to copy the variables which contain name like a or k in another dataset.how can we create the dataset?

Thank you,

John


Accepted Solutions
Solution
‎01-07-2012 05:01 PM
PROC Star
Posts: 7,474

Re: How to copy only selected variables from a Null dataset to another dataset?

The method I proposed works with null datasets.  e.g., try the following combination:

proc sql;

  create table class

     like sashelp.class

  ;

quit;

proc sql;

  create table want

     like class (keep=n: aSmiley Happy

  ;

quit;

Or am I misunderstanding what you are asking?

View solution in original post


All Replies
PROC Star
Posts: 7,474

Re: How to copy only selected variables from a Null dataset to another dataset?

One method although I'm sure there are many others:

proc sql;

  create table want

     like sashelp.class (keep=n: aSmiley Happy

  ;

quit;

Contributor
Posts: 35

How to copy only selected variables from a Null dataset to another dataset?

Sir, Your reply helped me find a solution.But, it is correct only when the source dataset is a normal dataset.

Following is the code:

data meng;

set sashelp.class(keep=n: aSmiley Happy;

run;

proc print data=meng;

run;

I am looking for a solution when Source dataset is a null dataset.

Thank you,

John

Solution
‎01-07-2012 05:01 PM
PROC Star
Posts: 7,474

Re: How to copy only selected variables from a Null dataset to another dataset?

The method I proposed works with null datasets.  e.g., try the following combination:

proc sql;

  create table class

     like sashelp.class

  ;

quit;

proc sql;

  create table want

     like class (keep=n: aSmiley Happy

  ;

quit;

Or am I misunderstanding what you are asking?

Contributor
Posts: 35

How to copy only selected variables from a Null dataset to another dataset?

Thanks for the reply @art297 and @Patrick.

Respected Advisor
Posts: 4,173

Re: How to copy only selected variables from a Null dataset to another dataset?

I believe there is some confusion around the term "null dataset".

Art seems to define it as "a dataset without rows" where John seems to talk about a "data _null_;" step.

@John

If above is true then please be aware that "data _null_;" means no dataset at all gets created so there can't be such a thing like a "_null_ dataset" as you define it.

You could do something like below to capture only variable definitions whithout writing any rows (=a dataset without any rows):

data Mapping(keep=n: aSmiley Happy;
  if 0 then output Mapping;
  set sashelp.class;
  /* ..... */
run;

proc contents data=Mapping;
quit;

Super User
Posts: 10,035

How to copy only selected variables from a Null dataset to another dataset?

Like what Patrick has been mentioned. Another way is:

data want;

if 0 then set class(keep=name sex);

stop;

run;

Ksharp

Super Contributor
Posts: 1,636

How to copy only selected variables from a Null dataset to another dataset?

Hi Kshart,

why you add "stop" in your code?

Thank you!

PROC Star
Posts: 7,474

How to copy only selected variables from a Null dataset to another dataset?

Linlin, KSharp is in a different time zone, thus I'll provide an answer in his place.  Using the method he proposed, there is no need to read the file.  Stop exits the program at that point without reading any records.

Super Contributor
Posts: 1,636

How to copy only selected variables from a Null dataset to another dataset?

Thank you Art!

I thought with "if 0 then set class(keep=name sex)", "stop" is not necessary.

PROC Star
Posts: 7,474

How to copy only selected variables from a Null dataset to another dataset?

Actually, you are right and I was wrong.  Given that syntax SAS will automatically stop the looping, thus the stop statement isn't needed.  However, it doesn't hurt to have it and will ensure that the datastep is stopped.

Super Contributor
Posts: 1,636

Re: How to copy only selected variables from a Null dataset to another dataset?

Hi Art,

Kshart and you are right. to get 0 observation, "stop" is necessary .

data dsn5; /* dsn5 has 0 observation */

  if _n_ =0 then set dsn3;

  stop;

run;

data dsn6; /* dsn6 has 1 observation with missing value for the variables */

  if _n_ =0 then set dsn3;

  run;

Thank you!

Linlin

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 1054 views
  • 3 likes
  • 5 in conversation