Why is not $ needed between the names of two last columns - sas

I can't get why there isn't $ between Age and Weight like there is for others
enter image description here
And Why the missing value is denoted by blank in row 2 but period(.) in row 4
enter image description here
Please help with such questions, I'm new to SAS and it's so miserable when I asked my colleagues, and nobody was willing to answer...

The $ after name and gender indicates that those variables are character variables. The other variables are numeric variables. Those are the only 2 data types in SAS.
Missing values for character variables are blanks while missing values for numeric variables are shown with a period.

The $ modifier in a INPUT statement let's SAS know that it should treat the preceding variable as CHARACTER instead of numeric. It is only needed when you have not previously already told SAS that the variable is character, because the default assumption is that a variable is numeric.
The normal character informat, $, which is what your code used because it did not specify any other informat for NAME or SEX, will convert a single period into blanks, which is how SAS stores an empty character value. If you want SAS to input a single period as an actual single period you would need to use the $CHAR informat instead. So if you modified the input statement like this:
input name $ sex :$char1. age weight ;
then the value of SEX would be the one byte string '.' (since the width of the informat gave SAS better information about what length to use to define the variable) instead of the current 8 character string of blanks ' '.
The normal way to display a missing numeric variable is as a single period. You can change to have it display some other single character instead by changing the setting of the MISSING option. For example to have the missing values of AGE print as blank also you could add this OPTIONS statement before you print it.
options missing=' ';

Related

How to remove underscore from column name after transposing data

How to remove underscore from column name after transposing data
there are around 80 90 columns of date and renaming each column is not the good choice , how to achieve it ?
Original data
Required data
SAS variable names follow SAS naming conventions, in your case a variable name can not start with a digit, but you can bypass this rule by using options validvarname=any; before using the transpose procedure. Default value for validvarname is V7.
From the SAS documentation: Rules for SAS Variable Names, when using options validvarname=V7; (default), variable name follows these rules:
The name can be up to 32 bytes in length.
The name can contain letters of the Latin alphabet, numerals, or underscores.
The name cannot contain blanks or special characters except for the underscore.
The name must begin with a letter of the Latin alphabet (A–Z, a–z) or the
underscore.
Trailing blanks are ignored. The name alignment is left-justified.
The name can contain mixed–case letters.
When using options validvarname=any; rules are:
The name can be up to 32 bytes in length.
The name can contain any characters, including blanks, national characters, special characters, and multi-byte characters. Names
containing these types of characters must be specified as name
literals.
The name can begin with any characters, including blanks, national characters, special characters, and multi-byte characters.
The name cannot contain any null bytes.
Leading blanks are preserved but trailing blanks are ignored.
The name must contain at least one character. A name with all blanks is not permitted.
It can contain mixed-case letters.
However, I would not recommend bypassing SAS naming conventions.
I would rather use the PREFIX optional argument to construct names for transposed variables.
For example, if PREFIX=DATE_, then the names of the variables would be DATE_31Dec2022, DATE_31Jan2023, …, DATE_31May2023.
If you want to produce that second photograph then produce it directly without creating an intermediate dataset. Then you do not need to try to force SAS to use date values as variable names.
proc report data=have;
columns system lan value,date ;
define system / group;
define lan / group;
define value / ' ';
define date / across order=internal format=date9. ' ';
run;

SAS variable value does not match the formatting specified

I have a SAS dataset. When I 'View Columns', I find a column with Type=text, length=3, informat = $3., format=$3.
The value stored in this variable is 10.
But based on the attributes, should it not be stored as 010?
The attributes say you have a character variable that can hold 3 bytes (normal character encodings use one byte per character). You could store '010' in that variable or '10 ' or even ' 10'. You could also store 'ABC' or 'abc'. It is just a character variable. Note that SAS always stores fixed length character fields so shorter values are padded with spaces.
It also has optionally added the FORMAT metadata saying that when displaying the value SAS should use the $3. format. Similarly is has optional metadata that says when reading text it should use the $3. informat to convert incoming text into the value to be stored.
This metadata is NOT needed because SAS already knows how to read and display character data. If you did store values with leading spaces you might want to attach the $CHAR3. format instead so that the leading spaces are preserved when writing the value.
As the variable is just text, it will just store what it is assigned. For example:
data have;
length var1 $ 3;
informat var1 $3.;
format var1 $3.;
input var1;
datalines;
10
010
;
The fact that it has a format of $3. will not cause it to be prefixed with a leading 0, as you will see from the documentation of the $w. format, where that is not mentioned. Also, the value could later be changed to 'ab'; in both cases the value is padded with a trailing space to make up the length of 3.

Rearrange text on SAS

I can not find the way to reverse text strings.
For example I want to reverse these:
MMMM121231M34 to become 43M132121MMMM
MM1M11M1 to become 1M11M1MM
1111213111 to become 1113121111
Judging from your examples, what you mean by 'rearrange' is actually 'reverse'.
In that case, you've got the very handy reverse() function in SAS.
Used in context:
data test;
length text $32;
infile datalines;
input text $;
result=reverse(strip(text));
datalines;
MMMM121231M34
MM1M11M1
1111213111
;
run;
EDIT on #Joe's request: in the particular example above, I create the test dataset by setting a length of 32 characters for the text variable. Therefore, when reading the values from datalines, these are padded with blanks up to that total of 32 characters. Hence, when reversing that value, the result has that many blanks at the start, followed by the actual value you are looking for. By adding the strip function, you remove the excess blanks from the value of text before reversing, keeping only the "real" value in the result.

What does an ampersand ("&") do in a put statement?

I'm familiar with the :, and ~ modifiers in SAS put and input statements. The behaviour of & in an input statement is also fairly well documented. But what does & do in a put statement?
It seems to have a similar effect to :, triggering modified list output rather than formatted output, but I can't find any documentation of this behaviour.
E.g.
data _null_;
set sashelp.class;
file 'c:\temp\output.csv' dlm=',';
put Name Sex Age & 4. Height Weight;
run;
Quoting from the on-line documentation in the section of SAS 9.4 under INPUT Statement, List
&
indicates that a character value can have one or more single embedded blanks. This format modifier reads the value from the next non-blank column until the pointer reaches two consecutive blanks, the defined length of the variable, or the end of the input line, whichever comes first.
Restriction:
The & modifier must follow the variable name and $ sign that it affects.
Tip:
If you specify an informat after the & modifier, the terminating condition for the format modifier remains two blanks.
Here is an example from the example section:
Example Reading Character Data That Contains Embedded Blanks
The INPUT statement in this DATA step uses the & format modifier with list input to read character values that contain embedded blanks.
data list;
infile file-specification;
input name $ & score;
run;
It can read these input data records:
----+----1----+----2----+----3----+
Joseph 11 Joergensen red
Mitchel 13 Mc Allister blue
Su Ellen 14 Fischer-Simon green
The & modifier follows the variable that it affects in the INPUT statement. Because this format modifier follows NAME, at least two blanks must separate the NAME field from the SCORE field in the input data records.
You can also specify an informat with a format modifier, as shown here:
input name $ & +3 lastname & $15. team $;
In addition, this INPUT statement reads the same data to demonstrate that you are not required to read all the values in an input record. The +3 column pointer control moves the pointer past the score value in order to read the value for LASTNAME and TEAM.

Where to specify input # in SAS?

In the following code
data temp2;
input id 1 #3 date mmddyy11.;
cards;
1 11/12/1980
2 10/20/1996
3 12/21/1999
;
run;
what do 1 #3 symbols mean ? i presume 1 means that id is the first character in the data . I know that #3 means that date variable starts with the third character , but why is it in front of date whereas 1 is after id?
Because that's a badly written input statement. You can specify input in a number of ways, and that mixes a few different ways to do things which happen to be allowed to mix (mostly). Read the SAS documentation on input for more information.
Some common styles that you can use:
input #1 id $5.; *Formatted input. Allows specification of start position and informat, more useful if using date or other informat that is not just normal character/number.;
input id str $ otherstr $ date :date9.; *List input. This is for delimited text (like a CSV), still lets you specify informat.
input #'ID:' id $5.; *A special case of formatted input. allows you to parse files that include the variable name, useful for old style files and some xml/json/etc. type files;
input x 1-10 y 11-20; *Column input. Not used very commonly as it's less flexible than start/informat style.;
There are other options (such as named input) that are not very frequently used in my experience.
In your specific example, the first variable is read in with column input [id 1 says 'read a 1 character numeric from position 1 into id'] and then the second variable is read with formatted input [#3 date mmddyy11. says 'Read an 11 character date variable from position 3[-13] into a numeric using the date informat to translate it to a number.'] It also says someone gave you that code who isn't very familiar with SAS, since mmddyy10. is the correct informat - the 11th character cannot be helpful.