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
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;
@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?
@ballardw Yes, good call.
I suppose the actual tables and variables are called something different, so just given as a model.
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,
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:
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:
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
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.