BookmarkSubscribeRSS Feed
mgorripati
Obsidian | Level 7

 

I am trying to convert a numeric variable to character , In case if it contains any numeric values . Strangely the IF statement gets executed even when the condition is not true and creates a blanl column .

 

Code :

 

DATA SASUSER.CLIENT_DATA;
SET sasuser.Raw_data;
ID_NUM = put(input(trim(ID_Code),best12.), z11. );
if index ne _N_ then delete;
if indicator_value in (1,2) THEN DO;
value =put(indicator_value,1.);
end;
RUN;

 

LOG :

 

22 GOPTIONS ACCESSIBLE;
23 DATA SASUSER.CLIENT_DATA;
24 SET sasuser.Raw_data;
25 ID_NUM = put(input(trim(ID_Code),best12.), z11. );
26 if index ne _N_ then delete;
27 if indicator_value in (1,2) THEN DO;
28 value =put(indicator_value,1.);
29 end;
30 RUN;
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
25:26
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
27:5
NOTE: Invalid numeric data, indicator_value='Y' , at line 27 column 5.
index=1 ID_Code=603459315 Client_Member=308312227
indicator_value=Y ID_NUM=00603459315 value=
_ERROR_=1 _N_=1

 

The new variable needs to be created only when indicator_value is 1 or 2 , but the highlighted note above shows that the IF block was executed and I can see a blank variable value in dataset.

 

 

8 REPLIES 8
ballardw
Super User

Variables do not exist conditionally in SAS datasets. The variable is either there or not. If no value is assigned then SAS will have a missing value.

Reeza
Super User

That's the correct behaviour. Even the if condition is going to have issues since its assuming the variables are numeric based on your code but it appears to be character, which is why you see the note about conversion. 

As long as a variable is referenced in a data step it's created. 

 

 

If you want to conditionally create a variable based on type you'll need macro logic. 

 

It may also be worth explaining what you're trying to do overall and we can make alternative suggestions. 

mgorripati
Obsidian | Level 7


Thanks for the explanation . 

The raw data file i  get contain  indicator_value either  Y,N or 1,2 (Character or Numeric) . I need to convert all to common data type so that i can do calculations by grouping records on  these categories  , create new variables by using this variable in case statement  and create a custom character format ( '1' = 'Y'  '2' ='N')  to use in proc report . 

My Case statements and proc report , which are written for character variable data , fails when  the program encounters numeric variable value .

 

I can not use both numeric and characater data in case statement (  IN ('Y','N','1','2',1,2)  ) ,  and can not have single format created for both charcater and numeric data  to use in proc report.

 

For Now , I used vtype that fits my requirement , but any other suggestions are appreciated.

 

DATA SASUSER.CLIENT_DATA;
SET sasuser.Raw_data;
ID_NUM = put(input(trim(ID_Code),best12.), z11. );
if index ne _N_ then delete;
if vtype(indicator_value) ='N' THEN
value =put(indicator_value,1.);
else value=indicator_value;
RUN;

 

Thanks,

MG

 

 

 

 

 

 

 

Reeza
Super User

Are you dealing with multiple files?  Otherwise a variable only holds one data type. 

I think you need to provide more information. 

Reeza
Super User

If they're text, not Excel, I would recommend fixing as many issues in import process rather than after the fact. 

ballardw
Super User

A custom informat could fix the issue when reading a file where the values vary from file to file but mean the same thing. BUT to assign an informat you will need to read it with a datastep. If you have been reading text files using proc import the log will have the code that you can copy and modify (and take some time to standardize text lengths and such).

 

A format that looks like:

 

proc format;

invalue $MyYesNo

'1','Y' = 'Y'

'2','N' = 'N'

;

run;

 

The datastep would include

informat indicator_value $MyYesNo. ;

before the input statement.

Again this would be modifying code generated by Proc Import (assuming these files are the same except for this behavior).

Fix a consistency problem as early as practical and it simiplifies stuff down stream.

 

BTW, I would read them into numeric and with values 1 for Yes and 0 for no as there are so many things that work better.

Some examples: Sum of such a coded variable will tell you the number of Yes, mean is percentage, code like "If variable then" works because the 1 is considered true and 0 false.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1954 views
  • 3 likes
  • 3 in conversation