BookmarkSubscribeRSS Feed
Ganeshk
Obsidian | Level 7

Hi, 

 

I have a 2 dataset. Let's say A and B. In which A data set contains only 1 variables (num_var) containing all numeric variables names (5 variable) of B data set.

On the other hand B data set contains 15 variables out of which 5 variable are numeric and 10 are character.  So all these 5 variables are listed in A dataset.

 

Now i need to create a macro which trigger to change all those listed in A data set (5 variable) to character for the B dataset.

 

Let me know if you need any clarification. 

 

Regards, 

Ganesh K 

 

 

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12
Why would you want to change internal representation?
It makes no meaningful change - adds no intelligence.
ChrisNZ
Tourmaline | Level 20

Like this?

data A; 
  NUM_VAR='VAR1';
data _null_;
  set A end=LASTOBS;
  if _N_=1 then call execute('data B; set B;');
  call execute(catt(NUM_VAR,'C=put(',NUM_VAR,',32.16); drop'));
  call execute(catt(NUM_VAR,'C; rename '||NUM_VAR,'C=',NUM_VAR,';'));
  if LASTOBS then call execute ('run;');
run;

1 + data B; set B;
2 + VAR1C=put(VAR1,32.16); drop
3 + VAR1C; rename VAR1C=VAR1;
4 + run;

 

ballardw
Super User

@ChrisNZ wrote:

Like this?

data A; 
  NUM_VAR='VAR1';
data _null_;
  set A end=LASTOBS;
  if _N_=1 then call execute('data B; set B;');
  call execute(catt(NUM_VAR,'C=put(',NUM_VAR,',32.16); drop'));
  call execute(catt(NUM_VAR,'C; rename '||NUM_VAR,'C=',NUM_VAR,';'));
  if LASTOBS then call execute ('run;');
run;

1 + data B; set B;
2 + VAR1C=put(VAR1,32.16); drop
3 + VAR1C; rename VAR1C=VAR1;
4 + run;

 


Wouldn't you actually be more likely to use

if _N_=1 then call execute('data NewB; set B;');

at least for development at least to prevent destroying the original data set while working out the actual PUT format desired?

 

ChrisNZ
Tourmaline | Level 20

@ballardw Yes, good call.

I suppose the actual tables and variables are called something different, so just given as a model.

Ganeshk
Obsidian | Level 7

Hi ChrisNZ,

 

Thanks for your response. I tried, but need different approach

 

Here is the below Data Set B : Lets consider sashelp.cars data set,

 

image.png

 

The highlighted Cars data set are numeric data variables.

 

Here is the listed Data Set A: Those highlight variable that of Data set B is captured as below:

 

image.png

 

Now using the macro 10 times automatically it need to to pick the variable from Data set A and Change Numeric to character in Data set B.

 

If i am converting numeric to character only for 1 variable, i will do code as below:

image.png

But i want the Variable name need to be picked up from Data set "A" and need to change in Data set B and saved as a new dataset.

 

Let me know on the query.

 

Thanks,

Ganesh K

 

andreas_lds
Jade | Level 19

@Ganeshk wrote:

Hi ChrisNZ,

 

....

 

But i want the Variable name need to be picked up from Data set "A" and need to change in Data set B and saved as a new dataset.

 



This is exactly what the code posted by @ChrisNZ does.

Ganeshk
Obsidian | Level 7

Hi Team,

 

I need to thank each and everyone, who took initiative to respond. 

 

Actually @ChrisNZ solution is looking more precise and efficient but unfortunately i am not able to decode the code with respect to sashelp.cars dataset. Currently i am using multiple loops and macro logic. some how i am getting the desired result. But if @ChrisNZ can re-code with the respect to sashelp.cars data. I would accept @ChrisNZ solution.

 

Thanks, awaiting for the reply,

 

Regards,

Ganesh K

ChrisNZ
Tourmaline | Level 20

There was no mention to SASHELP.CARS data in the original question, and changing the code I provided to use different table names should be trivial.

So I'll leave to you to do as an exercise. You are learning SAS, right? So welcome this opportunity to learn a new technique that's much cleaner than macros.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1197 views
  • 0 likes
  • 5 in conversation