07-20-2017
saslamber
Calcite | Level 5
Member since
06-16-2017
- 4 Posts
- 0 Likes Given
- 0 Solutions
- 0 Likes Received
-
Latest posts by saslamber
Subject Views Posted 1481 07-20-2017 10:32 AM 1516 07-17-2017 11:36 AM 3027 06-19-2017 03:46 AM 3082 06-16-2017 12:36 PM -
Activity Feed for saslamber
- Posted Re: How to rename a wide amount of variables with forbidden characters in their names? on SAS Programming. 07-20-2017 10:32 AM
- Posted How to rename a wide amount of variables with forbidden characters in their names? on SAS Programming. 07-17-2017 11:36 AM
- Posted Re: How to find strings from a table within strings from another table? on SAS Programming. 06-19-2017 03:46 AM
- Posted How to find strings from a table within strings from another table? on SAS Programming. 06-16-2017 12:36 PM
07-20-2017
10:32 AM
Both of your codes worked to solve my issue, my thanks.
... View more
07-17-2017
11:36 AM
Hi everyone, I have a table (named tabletest) with lots of variables (too numerous to be renamed one by one, for instance variables named a-1... a-10, ..., z-1... z-10, a, ..., z) whose names may have invalid characters (for instance an-1, which was coded as 'an-1'n to be accepted). My aim is to rename all these variables with invalid characters (we can assume - is the only one), by replacing the invalid characters by Z. I figured I would first get the list of the names of all variables from my table by exporting the result of a proc contents in a table named listname (where I only kept the names). I know I can use tranwrd (for instance name = TRANWRD(name, '-', 'Z') ; in a data set) to rename all elements of my list of variables listname. However, I do not know how to rename all the variables of tabletest that appear to have a - in their name. I tried what you can see below, but it didn't work because temp1 isn't a variable of tabletest in my code, only the values it takes are ones. DATA tabletest ;
INPUT 'an-1'n 'an-2'n 'an-3'n 'an-4'n 'an-5'n 'an-6'n 'an-7'n 'an-8'n b c d e ;
CARDS ;
1 3 5 7 9 11 13 15 17 19 21 23
2 4 6 8 10 12 14 16 18 20 22 24
;
RUN ;
DATA listname ;
INPUT name : $40. ;
CARDS ;
an-1
an-2
an-3
an-4
an-5
an-6
an-7
an-8
b
c
d
e
;
RUN ;
DATA _NULL_ ;
SET listname ;
temp1 = name ;
temp2 = TRANWRD(name, '-', 'Z') ;
IF temp2 NE temp1 THEN CALL execute('PROC DATASETS LIBRARY = work ; MODIFY tabletest ; RENAME temp1 = temp2 ;') ;
RUN ; What could I do to fix that issue? Thanks in advance for any proposal.
... View more
06-19-2017
03:46 AM
Thanks to both, both of your programs worked and solved my issue, so the help was much appreciated.
... View more
06-16-2017
12:36 PM
Hi everyone, here is my issue: I have two available tables: -a table list_words with one string variable named words) and 10 individuals (or p individuals in general, with p supposed as known) -a table raw_data with one string variable (maned text) and 20 individuals (or n individuals in general, with n supposed as known) I would like to create a new variable main_word within the raw_data table. For each individual i of the raw_data table, if one of the words (i.e. a value of the variable words of the raw_data table) can be found within the text, then the i-th element of raw_data takes the found word as a value for main_word. If no word is found within the text for the i-th individual, then main_word receives the value "" for that individual. is it possible to do such a thing without having to write a DATA step on the raw_data table for each of the words? (with the word manually entered as a parameter for an indexw function)? What I tried to do (and which didn't work): 1: Use an array and put each word in a macro-variable. DATA _NULL_ ;
SET list_words;
CALL SYMPUT(COMPRESS("wordsmacro"||_N_),words) ;
RUN ;
%LET array_word = array array_word $30 wordsmacro&1-wordsmacro&10 ;
DATA raw_data ;
SET raw_data ;
LENGTH main_word $ 30 ;
DO i = 1 to 10 ;
IF INDEXW(text, array_word[i]) > 0
THEN DO ;
main_word = array_word[i] ;
LEAVE ;
END ;
END ;
RUN ; The issue here is that array_word apparently isn't defined inside the DATA step. The log extract with the mistakes is the following: 27 IF INDEXW(text, array_word[i]) > 0 ERROR: Undeclared array referenced: array_word. ERROR: Variable array_word has not been declared as an array. 28 THEN DO ; 29 main_word = array_word[i] ; ERROR: Undeclared array referenced: array_word. ERROR: Variable array_word has not been declared as an array. 30 LEAVE ; 2: Put each word in a macro-variable and do a loop on the table: DATA _NULL_ ;
SET list_words ;
CALL SYMPUT(COMPRESS("wordsmacro"||_N_),words) ;
RUN ;
%MACRO parcours ;
DATA raw_data ;
SET raw_data ;
LENGTH main_word $ 30 ;
%DO i = 1 %TO 10 :
IF INDEXW(text, &&wordsmacro&i) > 0
THEN DO ;
main_word = &&wordsmacro&i ;
LEAVE ;
END ;
%END ;
RUN ;
%MEND ;
%parcours ; The log extract with the mistakes is the following: 23 %parcours ; WARNING: Apparent symbolic reference I not resolved. WARNING: Apparent symbolic reference MOTSMACRO not resolved. WARNING: Apparent symbolic reference I not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 10 : IF INDEXW(text, &&wordsmacro&i) > 0 THEN DO ERROR: The %TO value of the %DO I loop is invalid. ERROR: The macro PARCOURS will stop executing. 24 25 GOPTIONS NOACCESSIBLE; 26 %LET _CLIENTTASKLABEL=; 27 %LET _CLIENTPROJECTPATH=; 28 %LET _CLIENTPROJECTNAME=; 29 %LET _SASPROGRAMFILE=; 30 31 ;*';*";*/;quit;run; ____ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. 3: I don't know how to code it here, but maybe a hash object could work too? Some possible tables: DATA list_words ;
INPUT mots $ 1-40 ;
DATALINES ;
Lorem
ipsum
dolor
sit
amet
consectetur
adipiscing
elit
sed
do
RUN ;
DATA raw_data ;
INPUT texte $ 1-600 ;
DATALINES ;
Lorem ipsum dolor sit amet
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore
et dolore magna aliqua.
Lorem ipsum dolor sit amet
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore
et dolore magna aliqua.
Lorem ipsum dolor sit amet
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore
et dolore magna aliqua.
Lorem ipsum dolor sit amet
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore
et dolore magna aliqua.
Lorem ipsum dolor sit amet
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore
et dolore magna aliqua.
RUN ;
... View more