Hi
I have problem for coding character var in categorical var.
DATA Bla;
SET Bla;
IF area = "Chicago" THEN area = 1;
ELSE IF area = "New York City" THEN area = 2;
ELSE IF area = "DC/Maryland/Virginia" THEN area = 3;
ELSE area = . ;
RUN;
PROC FREQ DATA = Bla;
TABLE area;
RUN;
I don't get the categories in my table, jus a row with :
area Frequency Percent Cumulative Frequency Cumulative Percent
. 50000 100.00 50000 100.00
Thanks
I have a copy of the data.
I did change the name of the variable, but still doesn't work.
narea = 1;
narea =2;
.....
But for my other variable, i have not problem by coding :
DATA Bla;
SET Bla;
IF x_type = "AAA" THEN x_type=1;
ELSE IF x_type = "BBB" THEN x_type=2;
ELSE x_type= .;
Original data = Bla
var = area : Char
Data = Blabla
new var = narea : Num
I start over, delete the data set, import the data set, give the name of the variable narea , and it does not work.
@Phamhhm wrote:
Hi
I have problem for coding character var in categorical var.
DATA Bla;
SET Bla;
IF area = "Chicago" THEN area = 1;
ELSE IF area = "New York City" THEN area = 2;
ELSE IF area = "DC/Maryland/Virginia" THEN area = 3;
ELSE area = . ;
RUN;
PROC FREQ DATA = Bla;
TABLE area;
RUN;
I don't get the categories in my table, jus a row with :
area Frequency Percent Cumulative Frequency Cumulative Percent
. 50000 100.00 50000 100.00
Thanks
You should look at your log from the data step. I bet it has message about character value was converted to numeric some where.
I think that your actual data in BLA before this step had a numeric variable named area. In that case when you did
if area='Chicago' then
SAS attempted to change the text "Chicago" to a numeric value to compare the actual value of the variable area. Since all of your comparisons shown would fail to convert to numeric the result of each comparison was "false" and the only assignment possible was the final assignment to missing.
Perhaps you had a different variable intended to compare such as CITY or similar?
But as @Reeza says the original data set BLA has been overwritten and may not be useable for your purpose.
Especially when recoding an existing variable the use of the
Data bla;
set bla;
is extremely dangerous as logic errors, such as in this example, have the potential of destroying you data.
Best is to create a new data set.
Repeated use of the structure also makes it extremely difficult to debug where something may have gone wrong as you overwrite the complete set at each use.
I also did change the DATA set name and it doesn't work.
DATA Blabla;
SET Bla;
Please check your input by running
proc freq data=Bla; table area; run;
Are there the areas you are looking for? Do they match upcase/locase letters exactly ?
and as said before:
- change output dataset name to differ from input
- define the category variable name different from current variables in dataset
1). SAS doesn't care about upper case or lower case.
2). Given a new name to a dataset is to prevent losing the original dataset.
3). Given a new name to a variable after transformation/manipulation is to prevent losing the old variable.
All the 3 little details are not the source of my problem, because I have try and you could try it to.
Anyways, here are the code , as you can see...
Thanks for the help.... People are not working all the time, people get out and go eat.
***********************************************************************************************************
DATA PHAM.Blabla;
SET PHAM.Bla;
IF div_type = "BTH" THEN div_type = 1;
ELSE IF div_type = "LDD" THEN div_type = 2;
ELSE IF div_type = "LTD" THEN div_type = 3;
ELSE div_type = .;
RUN;
***************************************************************************************************************
NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
93:42 94:42 95:42 96:42
NOTE: There were 50000 observations read from the data set PHAM.BLA.
NOTE: The data set PHAM.BLABLA has 50000 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
***************************************************************************************************************
PROC FREQ DATA = PHAM.Blabla;
TABLE div_type;
RUN;
***************************************************************************************************************
The SAS System |
40721 | 81.44 | 40721 | 81.44 |
963 | 1.93 | 41684 | 83.37 |
7855 | 15.71 | 49539 | 99.08 |
461 | 0.92 | 50000 | 100.00 |
***************************************************************************************************************************************
The SAS System |
The SAS System |
PHAM.BLA | 50000 |
DATA | 2 |
V9 | 0 |
2018-11-15 19:15:41 | 33 |
2018-11-15 19:15:41 | 0 |
NO | |
NO | |
WINDOWS_64 | |
wlatin1 Western (Windows) |
65536 |
26 |
1 |
1977 |
1940 |
0 |
YES |
C:\Program Files\Oxford\HEC\Techniques Exploitation Données\Travail2\bla.sas7bdat |
9.0401M5 |
X64_10HOME |
DESKTOP-MSHHIQ0\Minh Pham |
2MB |
1769472 |
area | Char | 30 | $F30. | $F30. | Area |
div_type | Char | 3 | $F3. | $3. | Division Type Code |
PHAM.BLABLA | 50000 |
DATA | 3 |
V9 | 0 |
2018-11-15 19:25:27 | 48 |
2018-11-15 19:25:27 | 0 |
NO | |
NO | |
WINDOWS_64 | |
wlatin1 Western (Windows) |
65536 |
37 |
1 |
1361 |
1333 |
0 |
YES |
C:\Program Files\Oxford\HEC\Techniques Exploitation Données\Travail2\blabla.sas7bdat |
9.0401M5 |
X64_10HOME |
DESKTOP-MSHHIQ0\Minh Pham |
2MB |
2490368 |
area | Char | 30 | $F30. | $F30. | Area |
div_type | Char | 3 | $F3. | $3. | Division Type Code |
narea | Num | 8 |
@Phamhhm wrote
1). SAS doesn't care about upper case or lower case.
that's right dealing with variable names.
that's wrong dealing with variable value.
The dataset is available, the code and the log output are also posted as requested .
Does anyone know how to figure out the solution to my problem that can help a beginner in SAS....
Solution is found, problem is solved.
Thank you for all your advices.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.