BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
john83
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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: a:)

  ;

quit;

Or am I misunderstanding what you are asking?

View solution in original post

11 REPLIES 11
art297
Opal | Level 21

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

proc sql;

  create table want

     like sashelp.class (keep=n: a:)

  ;

quit;

john83
Calcite | Level 5

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: a:);

run;

proc print data=meng;

run;

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

Thank you,

John

art297
Opal | Level 21

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: a:)

  ;

quit;

Or am I misunderstanding what you are asking?

john83
Calcite | Level 5

Thanks for the reply @art297 and @Patrick.

Patrick
Opal | Level 21

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: a:);
  if 0 then output Mapping;
  set sashelp.class;
  /* ..... */
run;

proc contents data=Mapping;
quit;

Ksharp
Super User

Like what Patrick has been mentioned. Another way is:

data want;

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

stop;

run;

Ksharp

Linlin
Lapis Lazuli | Level 10

Hi Kshart,

why you add "stop" in your code?

Thank you!

art297
Opal | Level 21

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.

Linlin
Lapis Lazuli | Level 10

Thank you Art!

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

art297
Opal | Level 21

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.

Linlin
Lapis Lazuli | Level 10

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 11 replies
  • 2521 views
  • 3 likes
  • 5 in conversation