How SAS gurus,
I have a simple question but couldn't figure it out. Not sure if it is doable but your help will be appreciated.
If a data set has two columns, one is the value, the other is the format, how can I created a third Column using the value of the two?
proc format ;
value go
0='no' 1='yes';
value to
0='missing' 1='present';
run;
DATA temp;
INPUT value variable $ ;
DATALINES;1 go
1 to;
My goal is to crate a third variable new using put(value,variable.) as a formula.
I have tried using sql to created a formula, in this example, put(1.go.) and put(1, to.) and how can I made data looks like:
value variable new
1 go yes
1 to present ;
Thanks!
SAS needs to compile the data step before it starts running. If you have CODE in DATA then you will need to first run a step to generate a complete data step. Then SAS can compile it and run it. You can use CALL EXECUTE( ) or write the code to a file and use %INCLUDE to run it.
data have;
input variable $ value decode $20.;
cards;
go 0 put(0,go.)
go 1 put(1,go.)
to 0 put(0,to.)
to 1 put(1,to.)
;
proc format ;
value go 1='Go' other='Stay';
value to 1='There' other='Here';
run;
filename code temp;
data _null_;
set have ;
file code ;
put 'if ' _n_ = ' then new=' decode ';' ;
run;
data want;
set have ;
length new $20. ;
%include code/source2;
run;
Obs variable value decode new 1 go 0 put(0,go.) Stay 2 go 1 put(1,go.) Go 3 to 0 put(0,to.) Here 4 to 1 put(1,to.) There
Log
1450 data want; 1451 set have ; 1452 length new $20. ; 1453 %include code/source2; NOTE: %INCLUDE (level 1) file CODE is file .../#LN00047. 1454 +if _N_=1 then new=put(0,go.) ; 1455 +if _N_=2 then new=put(1,go.) ; 1456 +if _N_=3 then new=put(0,to.) ; 1457 +if _N_=4 then new=put(1,to.) ; NOTE: %INCLUDE (level 1) ending. 1458 run; NOTE: There were 4 observations read from the data set WORK.HAVE. NOTE: The data set WORK.WANT has 4 observations and 4 variables.
Hi,
Welcome to SAS Community.
Can you be more specific, it is not so clear what your trying to do here. Simply present the data you have and what is the output your expecting.
Thanks for your quick response!
I received a data set with two separated variables as my example shown above,. I'm trying to create one new variable using values existing two existing variables.
current data:
Variable Value
go 1
to 1
I wanted to create a new variable called new using values from Variable and Value, the output should be
Variable Value New
go 1 yes
to 1 present
Thanks again.
The PUTN() and PUTC() functions can use an expression for the name of the format.
First let's make your formats using names that are more descriptive of what they do. Let's also make a format we can use to associate which format is used for which variable name.
proc format ;
value yesno 1='yes' other='no';
value present 1='present' other='missing';
value $format 'GO'='YESNO.' 'TO'='PRESENT.' ;
run;
Now let's make some test data:
data temp;
length variable $32 value 8;
do variable='go','to';
do value=0,1;
output;
end;
end;
run;
And now let's figure out what decode to generate for each value/variable pair.
data want ;
set temp;
length decode $50 ;
decode = putn(value,put(upcase(variable),$format.));
run;
Obs variable value decode 1 go 0 no 2 go 1 yes 3 to 0 missing 4 to 1 present
Thanks so much, your code works for that task!
One step further, if put(value,variable.) string have been generated in third variable, how can we retrieve the value? in other words, how can we make the third variable run? Taking the same data as an example, if a table is given below, how can we let decode run and get the correct format?
Thanks!
Obs variable value decode 1 go 0 put(0,go.)
2 go 1 put(1,go.) 3 to 0 put(0,to.) 4 to 1 put(1,to.)
SAS needs to compile the data step before it starts running. If you have CODE in DATA then you will need to first run a step to generate a complete data step. Then SAS can compile it and run it. You can use CALL EXECUTE( ) or write the code to a file and use %INCLUDE to run it.
data have;
input variable $ value decode $20.;
cards;
go 0 put(0,go.)
go 1 put(1,go.)
to 0 put(0,to.)
to 1 put(1,to.)
;
proc format ;
value go 1='Go' other='Stay';
value to 1='There' other='Here';
run;
filename code temp;
data _null_;
set have ;
file code ;
put 'if ' _n_ = ' then new=' decode ';' ;
run;
data want;
set have ;
length new $20. ;
%include code/source2;
run;
Obs variable value decode new 1 go 0 put(0,go.) Stay 2 go 1 put(1,go.) Go 3 to 0 put(0,to.) Here 4 to 1 put(1,to.) There
Log
1450 data want; 1451 set have ; 1452 length new $20. ; 1453 %include code/source2; NOTE: %INCLUDE (level 1) file CODE is file .../#LN00047. 1454 +if _N_=1 then new=put(0,go.) ; 1455 +if _N_=2 then new=put(1,go.) ; 1456 +if _N_=3 then new=put(0,to.) ; 1457 +if _N_=4 then new=put(1,to.) ; NOTE: %INCLUDE (level 1) ending. 1458 run; NOTE: There were 4 observations read from the data set WORK.HAVE. NOTE: The data set WORK.WANT has 4 observations and 4 variables.
Awesome! This is so helpful!
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.