So I do the below, and it quotes some variables when they have a comma in them.
I want put quotes on selective column like Horsepower ONLY.
Please adivse.
data _null_;
file "/path/output/unix/cars.csv"
dsd dlm = ',';
set sashelp.cars;
if _n_ = 1 then put @1
"Make,Model,Type,Origin,DriveTrain,MSRP,Invoice,EngineSize,Cylinders,Horsepower,MPG_City,MPG_Highway,Weight,Wheelbase,Length";
put Make
Model
Type
Origin
DriveTrain
MSRP
Invoice
EngineSize
Cylinders
Horsepower
MPG_City
MPG_Highway
Weight
Wheelbase
Length;
run;
You can force PUT to add quotes even when the value being written does not need them by using the ~ modifier.
Example: You see that the values of MSRP require quotes because they contain the delimiter, but values for ENGINESIZE and MODEL do not.
428 data _null_; 429 set sashelp.cars(obs=5); 430 file log dsd ; 431 put 432 Make 433 Model 434 MSRP 435 EngineSize 436 Cylinders 437 Horsepower ~ 438 MPG_City 439 ; 440 run; Acura,MDX,"$36,945",3.5,6,"265",17 Acura,RSX Type S 2dr,"$23,820",2,4,"200",24 Acura,TSX 4dr,"$26,990",2.4,4,"200",22 Acura,TL 4dr,"$33,195",3.2,6,"270",20 Acura,3.5 RL 4dr,"$43,755",3.5,6,"225",18 NOTE: There were 5 observations read from the data set SASHELP.CARS.
But why would you want to make your output file larger than it needs to be?
You can force PUT to add quotes even when the value being written does not need them by using the ~ modifier.
Example: You see that the values of MSRP require quotes because they contain the delimiter, but values for ENGINESIZE and MODEL do not.
428 data _null_; 429 set sashelp.cars(obs=5); 430 file log dsd ; 431 put 432 Make 433 Model 434 MSRP 435 EngineSize 436 Cylinders 437 Horsepower ~ 438 MPG_City 439 ; 440 run; Acura,MDX,"$36,945",3.5,6,"265",17 Acura,RSX Type S 2dr,"$23,820",2,4,"200",24 Acura,TSX 4dr,"$26,990",2.4,4,"200",22 Acura,TL 4dr,"$33,195",3.2,6,"270",20 Acura,3.5 RL 4dr,"$43,755",3.5,6,"225",18 NOTE: There were 5 observations read from the data set SASHELP.CARS.
But why would you want to make your output file larger than it needs to be?
To match someone else's output for validation purposes.
Why this other person adds quotes around variables that don't need it-- I have no idea.
Quick question:
Is there a way to conditionally quote the values?
For example-- put quotes only if the horsepower is more than 250?
Use IF statement.
458 data _null_; 459 set sashelp.cars(obs=5); 460 file log dsd ; 461 put 462 Make 463 Model 464 MSRP 465 EngineSize 466 Cylinders 467 @ 468 ; 469 if horsepower > 250 then put Horsepower ~ @; 470 else put Horsepower @; 471 put 472 MPG_City 473 ; 474 run; Acura,MDX,"$36,945",3.5,6,"265",17 Acura,RSX Type S 2dr,"$23,820",2,4,200,24 Acura,TSX 4dr,"$26,990",2.4,4,200,22 Acura,TL 4dr,"$33,195",3.2,6,"270",20 Acura,3.5 RL 4dr,"$43,755",3.5,6,225,18 NOTE: There were 5 observations read from the data set SASHELP.CARS.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.