Same answer as via private message. Read the log.
It works, your PROC PRINT is wrong. The VAR statement does not support formats and the error pretty much tells you that.
ALWAYS READ THE LOG.
@shaher wrote:
data aaa;
input vartype $char9. stat1 stat2;
datalines;
numeric 25.5 12.7
character 36 34.4
numeric 150 124
charecter 23 12.1
;
run;
proc print data=HW4q2; run;
data bbb;
set aaa;
length newvar $ 20;
if vartype ='numeric' then newvar = catt(stat1, ' (' , stat2, ')' );
else newvar = catt(stat1, ' (', stat2, '%)' );
run;
proc print data=bbb;
var vartype $char9. newvar $ 20;
run;
this is the full code
What's the question for us?
What have you tried? What works, what doesn't?
To answer your question I'd have to type out the data, at minimum, providing that would be helpful for anyone who did want to help you out with this question. Don't post data or code as images.
@shaher wrote:
Hi
I'm trying to find a code to create a new character variable. by using trim function.
A character variable named myVar is created as soon as you say
myVar = "Some string";
I'm going to echo the suggestion that the source of this question may be not the most reliable.
Are you doing an exercise to learn to create new variables or how to create clinical reports?
If it's the latter, I recommend following Cynthia Zenders' Create Complex Reports document and the code that goes along with it. You can work through the examples and then decide how to create your reports.
You have numeric values that need to be converted to character, to make them part of the new variable. As a result, it's easier to forget about TRIM and use CATT instead:
length newvar $ 20;
if datatype='numeric' then newvar = catt(stat1, ' (' , stat2, ')' );
else newvar = catt(stat1, ' (', stat2, '%)' );
I use it but not working
length newvar $ 20;
if vartype ='numeric' then newvar = catt(stat1, ' (' , stat2, ')' );
else newvar = catt(stat1, ' (', stat2, '%)' );
run;
proc print data=HW4q2p1;
var vartype $char9. newvar $ 20;
run;
-----------------
The SAS System 17:54 Tuesday, September 18, 2018 6
Obs vartype stat1 stat2
1 numeric 25.5 12.7
2 character 36.0 34.4
3 numeric 150.0 124.0
4 charecter 23.0 12.1
this is my friend result but I don't how to get it
Is that your full code? Where's the DATA step statement?
Paste the full code. The code suggested would need to be embedded within a data step.
@shaher wrote:
I use it but not working
length newvar $ 20;
if vartype ='numeric' then newvar = catt(stat1, ' (' , stat2, ')' );
else newvar = catt(stat1, ' (', stat2, '%)' );
run;
proc print data=HW4q2p1;
var vartype $char9. newvar $ 20;
run;-----------------
The SAS System 17:54 Tuesday, September 18, 2018 6
Obs vartype stat1 stat2
1 numeric 25.5 12.7
2 character 36.0 34.4
3 numeric 150.0 124.0
4 charecter 23.0 12.1
this is my friend result but I don't how to get it
Same answer as via private message. Read the log.
It works, your PROC PRINT is wrong. The VAR statement does not support formats and the error pretty much tells you that.
ALWAYS READ THE LOG.
@shaher wrote:
data aaa;
input vartype $char9. stat1 stat2;
datalines;
numeric 25.5 12.7
character 36 34.4
numeric 150 124
charecter 23 12.1
;
run;
proc print data=HW4q2; run;
data bbb;
set aaa;
length newvar $ 20;
if vartype ='numeric' then newvar = catt(stat1, ' (' , stat2, ')' );
else newvar = catt(stat1, ' (', stat2, '%)' );
run;
proc print data=bbb;
var vartype $char9. newvar $ 20;
run;
this is the full code
Considering that the vartype is misspelled for the 4th record I would be very careful using their example data.
Also anytime a variable changes meanings like that, stat1 is sometimes mean and sometimes count, then someone is providing poor data models to follow...
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.