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

Hello all,

 

Hope you all well.

 

I have two libraries: 'SuperHero' and 'Height'

In library 'SuperHero', it has 1000 datasets with different names, for example, the 1000 datasets name are

Bumblebee,

Starscream,

Batman,

Wonderwoman,

...etc

 

In library 'Height', it has only one dataset, having two variables, one is each dataset's name in 'SuperrHero', another variable is their height, such as

Name                 Height

Bumblebee           14.8

Starscream           22.5

Batman                 1.86

Wonderwoman      1.72

....etc

 

What I would like to do is, add Height variable and value to each file in 'SuperHero'

Logic is

IF Name variable in 'Height' is the same with dataset's name in 'SuperHero',

THEN add Name's corresponding Height to that dataset in 'SuperHero'.

 

So that, in 'SuperHero', each obs of each dataset has a new variable called Height.

 

For example, originally, open Bumblebee data in 'SuperHero', it is like this

 Age                   Weight

23                          345

24                          349

25                          765

 

After what we do, open Bumblebee data in 'SuperHero', it will be like this

Height             Age                  Weight

14.8                   23                          345

14.8                   24                          349

14.8                   25                          765

 

Does this need macro?

Anyway, what code should I use?

 

Thank you and keep warm!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Yes it needs a macro. 

I would recommend you first do this for your 'base case' and get working code and then figure out how to make it dynamic and automatic. 

 

If you can assume all the data sets are listed in the HEIGHT data set then something as simple as this may be all you need. 

Untested. CALL EXECUTE has examples in the documentation if you can't get it working.

 

%macro add_height(libname=, dsn=, height=);

data &libname..&dsn;
set &libname..&dsn;

height = &height;
run;

%mend;

data _null_;
set height;

str = catt('%add_height(libname=', 'work', ', dsn=', dataset_name, ', height=', height, ');');

call execute(str);

run;

@yanshuai wrote:

Hello all,

 

Hope you all well.

 

I have two libraries: 'SuperHero' and 'Height'

In library 'SuperHero', it has 1000 datasets with different names, for example, the 1000 datasets name are

Bumblebee,

Starscream,

Batman,

Wonderwoman,

...etc

 

In library 'Height', it has only one dataset, having two variables, one is each dataset's name in 'SuperrHero', another variable is their height, such as

Name                 Height

Bumblebee           14.8

Starscream           22.5

Batman                 1.86

Wonderwoman      1.72

....etc

 

What I would like to do is, add Height variable and value to each file in 'SuperHero'

Logic is

IF Name variable in 'Height' is the same with dataset's name in 'SuperHero',

THEN add Name's corresponding Height to that dataset in 'SuperHero'.

 

So that, in 'SuperHero', each obs of each dataset has a new variable called Height.

 

For example, originally, open Bumblebee data in 'SuperHero', it is like this

 Age                   Weight

23                          345

24                          349

25                          765

 

After what we do, open Bumblebee data in 'SuperHero', it will be like this

Height             Age                  Weight

14.8                   23                          345

14.8                   24                          349

14.8                   25                          765

 

Does this need macro?

Anyway, what code should I use?

 

Thank you and keep warm!

 


 

View solution in original post

4 REPLIES 4
Reeza
Super User

Yes it needs a macro. 

I would recommend you first do this for your 'base case' and get working code and then figure out how to make it dynamic and automatic. 

 

If you can assume all the data sets are listed in the HEIGHT data set then something as simple as this may be all you need. 

Untested. CALL EXECUTE has examples in the documentation if you can't get it working.

 

%macro add_height(libname=, dsn=, height=);

data &libname..&dsn;
set &libname..&dsn;

height = &height;
run;

%mend;

data _null_;
set height;

str = catt('%add_height(libname=', 'work', ', dsn=', dataset_name, ', height=', height, ');');

call execute(str);

run;

@yanshuai wrote:

Hello all,

 

Hope you all well.

 

I have two libraries: 'SuperHero' and 'Height'

In library 'SuperHero', it has 1000 datasets with different names, for example, the 1000 datasets name are

Bumblebee,

Starscream,

Batman,

Wonderwoman,

...etc

 

In library 'Height', it has only one dataset, having two variables, one is each dataset's name in 'SuperrHero', another variable is their height, such as

Name                 Height

Bumblebee           14.8

Starscream           22.5

Batman                 1.86

Wonderwoman      1.72

....etc

 

What I would like to do is, add Height variable and value to each file in 'SuperHero'

Logic is

IF Name variable in 'Height' is the same with dataset's name in 'SuperHero',

THEN add Name's corresponding Height to that dataset in 'SuperHero'.

 

So that, in 'SuperHero', each obs of each dataset has a new variable called Height.

 

For example, originally, open Bumblebee data in 'SuperHero', it is like this

 Age                   Weight

23                          345

24                          349

25                          765

 

After what we do, open Bumblebee data in 'SuperHero', it will be like this

Height             Age                  Weight

14.8                   23                          345

14.8                   24                          349

14.8                   25                          765

 

Does this need macro?

Anyway, what code should I use?

 

Thank you and keep warm!

 


 

yanshuai
Quartz | Level 8

Sorry back to you late. But this works perfect.

Thank you Reeza.

Astounding
PROC Star

Not sure if this might be simpler to follow.  Using the HEIGHT data set, and assuming that a LIBNAME statement defines SuperHero as the folder holding all the SAS data sets:

 

data _null_;

set height;

call execute ('data superhero.' || name || '; set superhero.' || name || '; height=' || put(height, best8.) || '; run;') ;

run;

 

Since you are replacing permanent SAS data sets, be sure to have backups available during the testing phase in case something goes wrong!

novinosrin
Tourmaline | Level 20

@yanshuai You wrote- "Does this need macro? Anyway, what code should I use?"

 

If memory isn't a constraint, the same can be achieved using Hash look up. Nevertheless, not as easy as call execute to stack the sas code one by one. Of course, if you are big time hash object user so to speak, it is not hard. 

 

 

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
  • 4 replies
  • 2534 views
  • 0 likes
  • 4 in conversation