Hi,
I am pretty good at programming in SAS but I have been unable to determine a straight forward way to create new variables in a dataset using metadata. I am creating a process that creates datasets using a data dictionary but I would like to incorporate this dictionary for new variables as well.
Is there a way to create a data dictionary for variables that I want created during data processing that would be based off the variables in the data dictionary? I don't want to include a bunch of if/then statements. As I think about this I believe it may be possible to put if/then statements into a metadata table and run them from a sas macro but I wonder if there is a cleaner way to accomplish that.
let me know if I need to be clearer.
thanks.
Please provide a simple example of data set before and after your change using datalines code, that can be run by anyone as is, explaining what the logic / rules are for creating the new variable(s).
Amir.
Hi,
Thanks for the response. Below is a brief program for what I am trying to do.
data dict;
input var $ type $ length 2. varnum 2.;
datalines;
name char 10 1
dept char 10 2
age num 2 3
sex char 1 4
graduated char 1 5
;
data have;
input var1 $ var2 $ var3 2. var4 $ var5 $;
datalines;
John Sales 30 M Y
Mary Acctng 40 F N
;
%macro format;
proc sql noprint;
select var, type, length, varnum
into :mvar1- , :mtype1-, :mlength1-, :mvarnum1-
from dict;
%let count = &sqlobs;
quit;
data temp;
set have;
%do v = 1 %to &count;
rename var&v = &&&mvar&v;
%end; /* end generate variable type conversion */
run;
data want;
set temp;
if graduate = "Y" then school = "college";
run;
%mend;
%format;
I idea is that I have a data dictionary that includes metadata for the variables represented in a dataset. I use this data dictionary to manipulate the dataset (eg. rename variables, change data types, set variable lengths, etc.). In the example I added the if/then statement (if graduate = "Y" then school = "college") creating a new variable named "school". What I want to do is have this variable created from a data dictionary in the macro instead of me adding an if/then statement to create it. I have tried to add the if/then statement to the data dictionary and that works but I wonder if there is a cleaner way to do it.
The endgame for this idea is to have a data dictionary that I can add variables to the will get added to the dataset whenever the macro runs instead of adding if/then statements.
Hope that helps. Thanks.
I was unable to get the example code provided to complete successfully.
If you need to conditionally assign each new variable you create then you could try creating a separate data set that holds your logic, e.g.:
data logic;
infile datalines truncover;
input logic $char200.;
datalines;
if graduate = "Y" then school = "college"
;
Then any number of logic lines could be used for any number of new variables.
Regards,
Amir.
If your requirement is for single variables then perhaps an INFORMAT is appropriate to read data in a custom manner.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.