I'm trying to run the following code that creates a data table with a text string for the Dimension variable, however the variable values in the bal_cat dataset are all blank.
data bal_cat;
infile datalines delimiter=' ' truncover dsd;
length order 8.0 var_name $ 50;
length Dimension $ 200 ;
input order var_name :$ Dimension :$ ;
datalines;
1 agegrp1 "Age Group: <= 74"
2 agegrp2 "Age Group: 75-79"
3 agegrp3 "Age Group: 80-84"
4 agegrp4 "Age Group: 85-89"
5 agegrp5 "Age Group: 90+"
6 gender_f1 "Female"
;
run;
If you want to have embedded spaces (actually embedded delimiters) then you have three options.
1) Use fixed length fields.
2) Use the & modifier. That will require two delimiters to mark the start of the next field.
3) Use DSD option. That will allow you use quotes the protected delimiters in the value. But you cannot have more than one delimiter between the fields.
4) Use the DLMSTR= option on the INFILE statement to specify a multiple byte delimiter string.
data columns;
infile datalines truncover;
input order 1 var_name $ 2-14 Dimension $15-35 ;
*---+----1----+----2----+----3----+----0 ;
datalines;
1agegrp1 Age Group: <= 74
2agegrp2 Age Group: 75-79
3agegrp3 Age Group: 80-84
4agegrp4 Age Group: 85-89
5agegrp5 Age Group: 90+
6gender_f1 Female
;
data fixed;
infile datalines truncover;
input order 1. var_name $13. Dimension $21.;
*---+----1----+----2----+----3----+----0 ;
datalines;
1agegrp1 Age Group: <= 74
2agegrp2 Age Group: 75-79
3agegrp3 Age Group: 80-84
4agegrp4 Age Group: 85-89
5agegrp5 Age Group: 90+
6gender_f1 Female
;
data ampersand;
input order var_name :&$13. Dimension &$21.;
*---+----1----+----2----+----3----+----0 ;
datalines;
1 agegrp1 Age Group: <= 74
2 agegrp2 Age Group: 75-79
3 agegrp3 Age Group: 80-84
4 agegrp4 Age Group: 85-89
5 agegrp5 Age Group: 90+
6 gender_f1 Female
;
data dsd;
infile datalines dsd dlm='|' truncover;
length order 8 var_name $13 Dimension $21 ;
input order var_name Dimension ;
*---+----1----+----2----+----3----+----0 ;
datalines;
1|agegrp1|Age Group: <= 74
2|agegrp2|Age Group: 75-79
3|agegrp3|Age Group: 80-84
4|agegrp4|Age Group: 85-89
5|agegrp5|Age Group: 90+
6|gender_f1|Female
;
data dlmstr;
infile datalines dlmstr='|x|' truncover;
length order var_name $13 Dimension $21 ;
input order var_name Dimension ;
*---+----1----+----2----+----3----+----0 ;
datalines;
1|x|agegrp1|x|Age Group: <= 74
2|x|agegrp2|x|Age Group: 75-79
3|x|agegrp3|x|Age Group: 80-84
4|x|agegrp4|x|Age Group: 85-89
5|x|agegrp5|x|Age Group: 90+
6|x|gender_f1|x|Female
;
NOTE: Do not embed TAB characters in data (or programs for that matter) and definitely not in quoted string. They are too easily misunderstood by programmers and the programs you use to edit the file. In code you can use the hex literal '09'x to specify a TAB character.
PS There is no need to include decimal places when specifying a LENGTH. SAS can only store variables using whole numbers of bytes so you only need integer values.
If you want to have embedded spaces (actually embedded delimiters) then you have three options.
1) Use fixed length fields.
2) Use the & modifier. That will require two delimiters to mark the start of the next field.
3) Use DSD option. That will allow you use quotes the protected delimiters in the value. But you cannot have more than one delimiter between the fields.
4) Use the DLMSTR= option on the INFILE statement to specify a multiple byte delimiter string.
data columns;
infile datalines truncover;
input order 1 var_name $ 2-14 Dimension $15-35 ;
*---+----1----+----2----+----3----+----0 ;
datalines;
1agegrp1 Age Group: <= 74
2agegrp2 Age Group: 75-79
3agegrp3 Age Group: 80-84
4agegrp4 Age Group: 85-89
5agegrp5 Age Group: 90+
6gender_f1 Female
;
data fixed;
infile datalines truncover;
input order 1. var_name $13. Dimension $21.;
*---+----1----+----2----+----3----+----0 ;
datalines;
1agegrp1 Age Group: <= 74
2agegrp2 Age Group: 75-79
3agegrp3 Age Group: 80-84
4agegrp4 Age Group: 85-89
5agegrp5 Age Group: 90+
6gender_f1 Female
;
data ampersand;
input order var_name :&$13. Dimension &$21.;
*---+----1----+----2----+----3----+----0 ;
datalines;
1 agegrp1 Age Group: <= 74
2 agegrp2 Age Group: 75-79
3 agegrp3 Age Group: 80-84
4 agegrp4 Age Group: 85-89
5 agegrp5 Age Group: 90+
6 gender_f1 Female
;
data dsd;
infile datalines dsd dlm='|' truncover;
length order 8 var_name $13 Dimension $21 ;
input order var_name Dimension ;
*---+----1----+----2----+----3----+----0 ;
datalines;
1|agegrp1|Age Group: <= 74
2|agegrp2|Age Group: 75-79
3|agegrp3|Age Group: 80-84
4|agegrp4|Age Group: 85-89
5|agegrp5|Age Group: 90+
6|gender_f1|Female
;
data dlmstr;
infile datalines dlmstr='|x|' truncover;
length order var_name $13 Dimension $21 ;
input order var_name Dimension ;
*---+----1----+----2----+----3----+----0 ;
datalines;
1|x|agegrp1|x|Age Group: <= 74
2|x|agegrp2|x|Age Group: 75-79
3|x|agegrp3|x|Age Group: 80-84
4|x|agegrp4|x|Age Group: 85-89
5|x|agegrp5|x|Age Group: 90+
6|x|gender_f1|x|Female
;
NOTE: Do not embed TAB characters in data (or programs for that matter) and definitely not in quoted string. They are too easily misunderstood by programmers and the programs you use to edit the file. In code you can use the hex literal '09'x to specify a TAB character.
PS There is no need to include decimal places when specifying a LENGTH. SAS can only store variables using whole numbers of bytes so you only need integer values.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: